Uploader for Silverlight
Step 3 of 4: Implementing Server Code

In the last steps, you set up a Silverlight application and added code to your application to implement the client action and behavior, but if you run your application now, you get an error because the server code is not implemented. In this step we add code to your project to implement the server code.

To implement the server code, complete the following steps:

  1. In the Visual Studio Solution Explorer, right-click the QuickStartWeb solution and select the Add | New Item option. If you named your project something other than "QuickStart," right-click MyProjectWeb where "MyProject" is the name of your project.
  2. In the Add New Item dialog box, select the Generic Handler template and name the new class "photoUpload.ashx" (this is the Uri from the C1Uploader object in Step 2 of 4).
  3. Click Add to add the file to your project and close the Add New Item dialog box.
  4. Open the photoUpload.ashx file and replace the default implementation of the ProcessRequest method with the following code:
    Visual Basic
    Copy Code
     Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
         Const MAXFILES As Integer = 30 
    
         ' Clean up 
         Dim path As String = context.Request.MapPath("Pictures") 
         Dim files As String() = System.IO.Directory.GetFiles(path) 
         If files.Length >= MAXFILES Then 
             For Each fileName As String In files 
             System.IO.File.Delete(fileName) 
             Next 
         End If 
    
         ' Get files being uploaded 
         Dim i As Integer = 0 
         While i < context.Request.Files.Count AndAlso i < MAXFILES 
             Dim file As HttpPostedFile = context.Request.Files(i) 
             Try 
             ' Check that it really is an image by loading it into an Image object 
             Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(file.InputStream) 
    
             ' Save file to the Pictures folder 
             Dim fileName As String = context.Request.MapPath("Pictures/" & file.FileName) 
             file.SaveAs(fileName) 
             Catch 
             ' If it doesn't look like a valid image, skip request 
             Exit Try 
             End Try 
             i += 1 
         End While 
     End Sub 
    

     

    C#
    Copy Code
     public void ProcessRequest(HttpContext context) 
     { 
         const int MAXFILES = 30; 
         // Clean up 
         string path = context.Request.MapPath("Pictures"); 
         string[] files = System.IO.Directory.GetFiles(path); 
         if (files.Length >= MAXFILES) 
         { 
             foreach (string fileName in files) 
                 System.IO.File.Delete(fileName); 
         } 
    
         // Get files being uploaded 
         for (int i = 0; i < context.Request.Files.Count && i < MAXFILES; i++) 
         { 
             HttpPostedFile file = context.Request.Files[i]; 
             try 
             { 
                 // Check that it really is an image by loading it into an Image object 
                 System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream); 
    
                 // Save file to the Pictures folder 
                 string fileName = context.Request.MapPath("Pictures/" + file.FileName); 
                 file.SaveAs(fileName); 
             } 
             catch 
             { 
                 // If it doesn't look like a valid image, skip request 
                 break; 
             } 
         } 
     } 
    
    This code starts by cleaning up the images folder in case there are more than 30 images in it. In a real application you might do this differently, possibly by compressing old images or enforcing a per-user storage limit.
    After the clean-up, the code loops through the files in the context.Request.Files collection and checks that they really are image files. You already added a similar check when implementing the server part of the code, but it is important to protect both ends of the application. Finally, the code saves each image into the Pictures folder.

What You've Accomplished

You've successfully created a Silverlight application that uses the C1Uploader control to upload files to the server. In the next step we run the application and observe its run time interactions.

See Also

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback