Building a multiple file upload system in Silverlight requires a combination of client-side XAML/C# code to select and stream files, and a server-side handler (like an ASP.NET .ashx generic handler) to receive and save the data. Because Silverlight runs strictly in a client-side sandbox, it cannot directly access the local file paths or save files to the web server without this explicit bridge. 1. Client-Side Implementation (Silverlight)
The client side leverages OpenFileDialog with multi-select enabled to capture user files, then loops through them using WebClient or HttpWebRequest to push streams asynchronously to the server. Step A: Configure the File Dialog
To allow users to pick more than one file, initialize the OpenFileDialog and explicitly set its Multiselect property to true.
private void SelectFilesButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Filter = “All Files (.)|.|Images (.jpg;.png)|.jpg;.png”; if (fileDialog.ShowDialog() == true) { // Iterate through the collection of selected FileInfo objects foreach (FileInfo file in fileDialog.Files) { UploadFileAsync(file); } } } Use code with caution. Step B: Stream the Files Asynchronously
Because Silverlight restricts direct access to local system paths for security, you must use .OpenRead() to process the file data as a stream.
private void UploadFileAsync(FileInfo file) { // Append the filename to the query string safely UriBuilder serverUri = new UriBuilder(”http://yourserver.com”); serverUri.Query = string.Format(“filename={0}”, Uri.EscapeDataString(file.Name)); WebClient client = new WebClient(); // Open a read stream for the local file using (Stream fileStream = file.OpenRead()) { // Read file bytes into a buffer or open an upload stream client.OpenWriteCompleted += (s, e) => { if (e.Error == null && e.Result != null) { using (Stream outputStream = e.Result) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { outputStream.Write(buffer, 0, bytesRead); } } } }; client.OpenWriteAsync(serverUri.Uri); } } Use code with caution. 2. Server-Side Implementation (ASP.NET Handler)
The server requires a generic handler (.ashx) or a custom endpoint to process the incoming InputStream sent by the Silverlight client. Step A: Create UploadHandler.ashx
Implement IHttpHandler to pull the filename from the query string and write the raw input stream directly into your target directory.
using System; using System.IO; using System.Web; public class UploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { string filename = context.Request.QueryString[“filename”]; if (!string.IsNullOrEmpty(filename)) { // Sanitize the filename to prevent directory traversal vulnerabilities filename = Path.GetFileName(filename); string uploadPath = context.Server.MapPath(“~/UploadedFiles/”); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string fullPath = Path.Combine(uploadPath, filename); // Stream the incoming data directly to disk using (FileStream fs = File.Create(fullPath)) { Stream inputStream = context.Request.InputStream; byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, bytesRead); } fs.Flush(); } context.Response.ContentType = “text/plain”; context.Response.Write(“Upload Successful”); } } public bool IsReusable => false; } Use code with caution. Crucial Architectural Reminders