File Upload from Silverlight Client – A Generic http Handler to Upload File

From Silverlight client, you can upload files on server. For this you need a httphandler. Following is the code for the httphandler-

using System;
using System.Web;
using System.IO;
using System.Configuration;
namespace WebProject.Web
{
    public class FileUpload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)

        {
            string fileuploaded = context.Request.QueryString["uploadedfile"];
            using (FileStream fileStream = File.Create(context.Server.MapPath
            ("~/Uploads/" + fileuploaded)))
            {
                byte[] bytes = new byte[4096]; //100MB max
                int totalBytesRead;
                while ((totalBytesRead = context.Request.InputStream.Read(bytes, 0,
                bytes.Length)) != 0)
                {
                    fileStream.Write(bytes, 0, totalBytesRead);
                }
            }
        }

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

Create a handler (FileUpload.ashx) in the root of your web project and paste above code in FileUpload.ashx.cs.

Following code on btnUpload_Click event will upload file from Silverlight client to server-

private void btnUpload_Click(object sender, RoutedEventArgs e)
{
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = false;
            bool? userAccepts = openFileDialog.ShowDialog();
            if (userAccepts == true)
            {
                // Start the upload process
                UriBuilder uriBuilder = new UriBuilder("http://localhost/WebProject/
                FileUpload.ashx");
                uriBuilder.Query = string.Format("uploadedfile={0}",
                openFileDialog.File.Name);
                WebClient client = new WebClient();
                if (openFileDialog.File.Length < = 104857600) //100MB in bytes
                {
                   client.OpenWriteCompleted += (innerSender, innerE) =>
                    {
                        Stream inputStream = openFileDialog.File.OpenRead();
                        Stream outputStream = innerE.Result;
                        byte[] bytes = new byte[4096];
                        int totalBytesRead;
                        while ((totalBytesRead =
                        inputStream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            outputStream.Write(bytes, 0, totalBytesRead);
                        }
                        inputStream.Close();
                        outputStream.Close();
                        MessageBox.Show("File Uploaded!", "Message",
                        MessageBoxButton.OK);
                    };
                    client.OpenWriteAsync(uriBuilder.Uri);
                }
                else
                {
                    MessageBox.Show("Maximum 100MB file can be uploaded!", "Error",
                    MessageBoxButton.OK); return;
                }
            }
}

Do not forget to create a folder ‘Uploads’ in web project’s root and give necessary permission so that IIS process can write into this folder.

This entry was posted in C#, Knowledge Sharing, Silverlight. Bookmark the permalink.

Leave a Reply