This is a mirror of official site: http://jasper-net.blogspot.com/

Upload multiple files using Generic Handler in Silverlight 4

| Thursday, September 16, 2010
This article describes for you the basic use of generic handler and how to upload multiple files using generic handler in Silverlight.

What is Generic Handler? Generic handlers have an extension of ASHX. They're equivalent to custom handlers written in C Sharp or Visual Basic.NET in that they contain classes that fully implement IHttpHandler. They're convenient in the same way ASPX files are convenient. You simply surf to them and they're compiled automatically.

First of all make a new Silverlight project.
Now right click on web project and click "Add New Item" and add a Generic Handler"

By default Generic Handler has this code.

public class Handler2 : IHttpHandler
{
   public void ProcessRequest(HttpContext context)
   {
       context.Response.ContentType = "text/plain";
       context.Response.Write("Hello World");
   }
   public bool IsReusable
   {
       get
       {
           return false;
       }
   }
}

Change ProcessRequest method.

public void ProcessRequest(HttpContext context)
{
   var imageName = context.Request["file"];
   string str;
   using (StreamReader streamReader = new StreamReader(context.Request.InputStream))
   {
       str = streamReader.ReadToEnd();
       byte[] array = Convert.FromBase64String(str);
       using (System.IO.FileStream fileStream = new FileStream(context.Server.MapPath("~/uploadedimages/") + imageName, FileMode.Create))
       {
           fileStream.Write(array, 0, array.Length);
       }
   }
}

Now let's add a button MainPage.xaml.

<Grid x:Name="LayoutRoot" Background="Blue">
       <Button Content="Browse images" Height="23" HorizontalAlignment="Left" Margin="88,46,0,0" Click="button1_Click" Name="button1" VerticalAlignment="Top" Width="206" />      

Read more: C# Corner

Posted via email from .NET Info

0 comments: