the Silverlight Applcation and write to the server .
This is done via in conjection with the Asp.net handlers.
private void FileUpLoader() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = false; bool? retVal = openFileDialog.ShowDialog(); if (retVal != null && retVal == true) { UploadFile(openFileDialog.File.Name,openFileDialog.File.OpenRead()); tblStatus.Text = openFileDialog.File.Name; } else { tblStatus.Text = "Please select a file"; } }
private void UploadFile(string fileName, Stream data) { UriBuilder uriBuilder = new UriBuilder("http://localhost/SLMemoryLeakSample.Web/FileUploader.ashx"); uriBuilder.Query = string.Format("filename={0}", fileName); WebClient webClient = new WebClient(); webClient.OpenWriteCompleted += (sender, e) => { WriteStream(data, e.Result); e.Result.Close(); data.Close(); }; webClient.OpenWriteAsync(uriBuilder.Uri); }
private void WriteStream(Stream input, Stream output) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0) { output.Write(buffer, 0, bytesRead); } }
The FileUploader.ashx will be as following
public class FileUploader : IHttpHandler { #region FileUploader public void ProcessRequest(HttpContext context) { string filename = context.Request.QueryString["filename"].ToString(); using (FileStream fs = File.Create(context.Server.MapPath("~/App_Data/" + filename))) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0) { fs.Write(buffer, 0, bytesRead); } } } public bool IsReusable { get { return false; } } #endregion }
Enjoy Coding ............................................
0 comments:
Post a Comment