Uploader for Silverlight Quick Start > 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:
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; } } } |
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.