|  Login
 Silverlight FileUploader

A Silverlight File Uploader

Silverlight File Uploader is a DotNetNuke 4 & 5 module that is a "wrapper" around the Open Source project Silverlight File Upload. It adds upload security that the original project does not currently have (as of January 2009. The application already detects if a user is authenticated, but that wont work for DNN because then any user could upload files, not just the ones you want). The DotNetNuke module does not alter the FileUpload.xap file so it should be possible to update the module when the original project has an update by simply replacing that file.

The Module

When you install the module, and you are logged in as a administrator, you will see the Settings link.

This takes you to the screen that allows you to set the password that will be used for the file transfers. A random password will automatically be created, but you can change it using this screen.

Clicking on the Select Files link from the main page brings up the file upload selection dialog that allows you to select multiple files. The default filter in the file selector is .jpg and .gif, but you can change the filter selector to "All Files *.*" to upload a file of any type and size.

The selected files will display in the upload status window.

Clicking the Display Thumbnails box will display a thumbnail of any image file.

Click Upload to upload the files.

After the files are uploaded, click the Files link...

...to see the uploaded files. Clicking the file name will allow you to download the file.

The Code

The DotNetNuke module is simply a "wrapper" around the original Silverlight File Upload project. It does add security so that only file uploads that use the correct password are accepted.

            string strAshxPath = String.Format(@"http://{0}/{1}/FileUpload.ashx?IDString={2}",
                Request.Url.Host, this.TemplateSourceDirectory, Utility.GetPassword());
            string strFilter = "Images (*.jpg;*.gif)|*.jpg;*.gif|All Files (*.*)|*.*";

            string SilverlightInitParameters = string.Format(@"UploadPage={0},UploadChunkSize={1},
            MaximumTotalUpload={2},MaximumUpload={3},MaxConcurrentUploads={4},ResizeImage={5},
            ImageSize={6},Multiselect={7},Filter={8},AllowThumbnail={9},JavascriptCompleteFunction={10},
            MaxNumberToUpload={11}",
                strAshxPath, "4194304", "-1", "-1", "1", "false", "300", "true", strFilter, "true", 
            "OnComplete", "-1");

            SilverlightFileUploadControl.InitParameters = SilverlightInitParameters;

The code above retrieves the file transfer password and sets the initialization parameters on the ASP.NET control that launches the Silverlight File Upload application. One of the parameters passed to the Silverlight application is the url for the .ashx file handler that receives the file stream from the Silverlight application. The password is appended to the url as the IDString parameter.

<%@ WebHandler Language="C#" Class="ADefwebserver.Modules.SilverlightFileUpload.FileUpload" %>
// see License.txt
using System;
using System.Web;

namespace ADefwebserver.Modules.SilverlightFileUpload
{
    public class FileUpload : IHttpHandler
    {
        private HttpContext ctx;
        public void ProcessRequest(HttpContext context)
        {
            ctx = context;
            string uploadPath = context.Server.MapPath("~/DesktopModules/SilverlightFileUpload/Upload");
            ADefwebserver.Modules.SilverlightFileUpload.FileUploadProcess fileUpload = new FileUploadProcess();
            fileUpload.FileUploadCompleted += new FileUploadCompletedEvent(fileUpload_FileUploadCompleted);
            fileUpload.ProcessRequest(context, uploadPath);
        }

        void fileUpload_FileUploadCompleted(object sender, FileUploadCompletedEventArgs args)
        {
            string id = ctx.Request.QueryString["id"];
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

The FileUpload.ashx file uses the code above to receive the file transmission from the Silverlight application and pass the contents of the file to the ProcessRequest method of the FileUploadProcess() class.

        public void ProcessRequest(HttpContext context, string uploadPath)
        {
            // Check Password
            string IDString = context.Request.QueryString["IDString"];
            if (IDString != Utility.GetPassword())
            {
                return;
            }

The ProcessRequest method checks the value passed in the IDString parameter with the file transfer password. If the password does not match, the transmission is rejected. 

Summary

This module demonstrates that the old restrictions on client uploads no longer applies. It is now possible to create applications that allow your users to upload large files. This opens up a wide range of solutions that were previously not possible.

If you would like to support the development of the Silverlight File Upload application, you may donate using PayPal (this goes to the original author not to me).

 Download

Requires ASP.net 3.5 SP1 or higher on the server

Use the install directions here:

http://dnnsilverlight.adefwebserver.com/FAQ/InstallingSilverlight/tabid/80/Default.aspx

Note: in IIS you will also need to set the MIME Type: Setting .xap MIME Type for Silverlight 2.0