File Upload in Silverlight 3

22. September 2009 17:43

I was searching for a simple file upload control for silverlight 3 and came across some excellent multi-file upload controls, but that is not what I needed.  Of course, to save some time I didn't want to write everything from scratch when so much excellent code was out there already.  And I'm a beginner when it comes to this.  So what I did was take a project and slim it down to the bare minimum of what I would need to just upload a file with a progress bar.  I used this project as the base and stripped it down and modified a few things to get it to work within 1 xaml file and use only the HttpHandler.

The project below is a terrific multifile upload solution and I recommend you download it and use it, or least download it and learn from it. I used it as the base for the code I attached.

http://slfileupload.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30368



The concept for what I have attached to this blog is the following:

1 XAML file with the following:
 - Title Field
 - Description Field
 - Button to Upload File
 - progress bar
 - text for messages

1 Web Project with the following:

 - HttpHandler for handling the upload
 - Test page for running Silverlight App
 - Directory to store uploads


I won't blog on how the entire project works, but here are a few notes on a few methods within it:

UpdateShowProgress within FileUpload.xaml.cs is where you can tell when it's complete and add more code to your liking as to what to do at that point.

GetUploadFolder in FileUpload.ashx has a switch statement based on a parameter from the query string provided in StartUpload(FileInfo file) in the xaml file. This can be used to store files in different locations (i.e., docs, images, etc...)

Note: I don't have a setting in the web.config for this project for the document path so it uses the default in GetUploadFolder()

Note: You may have to change the URI in StartUpload in FileUpload.xaml.cs


Here are some screenshots below:

 







FileUpload.zip (1.97 mb)

Tags:

Silverlight

blog comments powered by Disqus



My Random Thought

I think the OCW is a great thing to have available to those who are in school, just finished school or just want to educate themself

http://ocwconsortium.org/

John On Twitter

Discounts