Home > View Post

Updated Silverlight Uploader for SL2 Beta 1

UPDATE - this has now been updated for SL2 Beta 2.

Back in August 2007 I posted the code for a file uploader written in Silverlight 1.1 Alpha (heady days). Since installing SL2 beta 1 I've really missed being able to use my file uploader to upload pictures to my blog.

Finally though, I've managed to find some time to update the uploader to use the new beta 1 pieces. The word 'update' probably isn't applicable as so so much has changed since the Alpha.

But all that change is almost entirely for the better; now we have:

  • real threading support - meaning we can avoid the timer hack from the previous version
  • access to the javascript alert and confirm dialogs directly in managed code
  • better support for web services with 'Add Service Reference' proxy creation

File Upload Dialog

Notice we still have the ability to select multiple files and configure the file Filter options (highlighted).

For reasons of reusability I bundled the uploader as a separate UserControl so it could be used in other silverlight applications. You can download the source below. Once again, the control uses a web service that takes a byte array. There had to be one small change to the service this time. Which, sadly, was to add a return code to indicate whether the upload was successful or whether the file already exists. This isn't something I like (as long term readers will recall) but SL2 beta 1 is unable to process SOAP faults - so this is a workaround.

Here's the source for the web service (all a little hard-coded but you get the idea):

[WebService(Namespace = "urn:schemas-thejoyofcode-com:upload:2007-08")]
public class Upload : WebService
    private static string _uploadPath = HttpContext.Current.Server.MapPath("~/Uploads");

    /// <summary>
    /// Uploads a byte array and writes to disk with specified fileName
    /// </summary>
    /// <param name="fileName">the name of the file</param>
    /// <param name="overwriteExisting">If a file with the specified name exists, should it be overwritten</param>
    /// <param name="fileData">The file data</param>
    /// <returns>An enum to indicate whether the upload succeeds or, if overwrite wasn't specified, if the file already exists.
    /// I don't like it but Silverlight 2 does not support SOAP faults</returns>
    public UploadStatus UploadFile(string fileName, bool overwriteExisting, byte[] fileData)
        string fullFilePath = Path.Combine(_uploadPath, fileName);
        // Make sure file does not already exist on the server.
        if (File.Exists(fullFilePath) && !overwriteExisting)
            return UploadStatus.FileExists;

        using (FileStream fs = new FileStream(fullFilePath, FileMode.Create))
            fs.Write(fileData, 0, fileData.Length);
        return UploadStatus.Succeeded;

    public enum UploadStatus

File Upload Dialog

As before, the user will be prompted if the file already exists. Arguably, this is sub-optimal and we should check if the file exists before we upload all the data but that is expected to be the exception rather than the rule.

Using the control is pretty simple:

<src:FileUploader ServiceUri="Upload.asmx" Filter="Image Files (*.gif;*.png;*.jpg;*.jpeg)|*.gif;*.png;*.jpg;*.jpeg|All Files|*" />

There are just two properties to worry about: ServiceUri which is the path to the web service (relative or absolute) and the Filter property specifies the filter used by the OpenFileDialog.


Anyway, rather than just looking at some screenshots why not try it for yourself. Note: this is a 'dummy' version of the uploader - I can't afford the bandwidth or the storage! It just simulates an upload by sleeping for 550ms per file.

You might be unable to view the demo in your aggregator - just click here to view the original post.

Source Code

As usual, this is SAMPLE CODE and has not been optimised. It has been written in a short time for a personal project and as such hasn't been subject to a great deal of testing. The content is provided "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Tags: Silverlight

Josh Post By Josh Twist
2:01 PM
20 May 2008

» Next Post: Lambda and Expression Trees
« Previous Post: Func and Action

Comments are closed for this post.

Posted by Dreamer @ 23 May 2008 2:24 AM
I don't understand this <src:FileUploader ServiceUri="Upload.asmx" Filter="Image Files....

how to use in ASP.NET

Posted by Josh @ 23 May 2008 2:26 AM
Hi Dreamer,

That snippet is XAML not ASP.NET. You need to get familiar with Silverlight - there are some great Getting Started guides at www.silverlight.net



© 2005 - 2021 Josh Twist - All Rights Reserved.