Use HttpResponse to load data into Silverlight

How to use asynchronous bidirectional binary loading and data loading in Silverlight?

  • I need to upload my binary to the server, and then>
  • Get response as well as binary from server

My current sample that I use is this: http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetresponse.aspx

But the sample starts with a request, and I need to start by downloading.

I tried with WebClient, but there I was not able to do the upload / download in the same sequence: Bidirectional use of Webclient for binary files

+4
source share
2 answers

I did it synchronously with handlers. (ASHX) in Silverlight.RadFileUploader is a Telerik component that works with these handlers.

namespace MyNameSpace.Web { public class FileHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.User.Identity.IsAuthenticated) { string fileName = context.Request.QueryString.Get("fileName"); if (File.Exists(fileName)) { context.Response.ContentType = MimeType(fileName); context.Response.AddHeader("Content-Transfer-Encoding", "binary"); context.Response.WriteFile(fileName, 0, length); } } } 

I cut most of the code, but it gives an idea. I used a handler to play audio files and a handler to load an audio file. Mimetic is a very important part. Your headlines are also important. This way you communicate what you want to do.

For asynchronous this page may help. http://msdn.microsoft.com/en-us/library/ms227433(v=vs.100).aspx

EDIT

The next handler file reads the file "Top.jasper" from the directory and writes it using the callback method. (ar.IsCompleted) argument checks to see if it is complete.

<%@ WebHandler Language="C#" CodeBehind="BDTest.ashx.cs" Class="AHBSBus.Web.Classes.BDTest" %>

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Threading; using System.IO; namespace AHBSBus.Web.Classes { /// <summary> /// Summary description for BDTest /// </summary> public class BDTest : IHttpAsyncHandler { public bool IsReusable { get { return false; } } public BDTest() { } public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) { cb = new AsyncCallback((ar) => { if (ar.IsCompleted) { var result = ar.AsyncState; File.WriteAllBytes("c:\\new.jasper", (byte[])result); } }); extraData = File.ReadAllBytes("c:\\Top.jasper"); context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n"); AsynchOperation asynch = new AsynchOperation(cb, context, extraData); asynch.StartAsyncWork(); return asynch; } public void EndProcessRequest(IAsyncResult result) { } public void ProcessRequest(HttpContext context) { var ctx=context; } } class AsynchOperation : IAsyncResult { private bool _completed; private Object _state; private AsyncCallback _callback; private HttpContext _context; bool IAsyncResult.IsCompleted { get { return _completed; } } WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } } Object IAsyncResult.AsyncState { get { return _state; } } bool IAsyncResult.CompletedSynchronously { get { return false; } } public AsynchOperation(AsyncCallback callback, HttpContext context, Object state) { _callback = callback; _context = context; _state = state; _completed = false; } public void StartAsyncWork() { ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null); } private void StartAsyncTask(Object workItemState) { //You may modify _state object here _context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n"); _context.Response.Write("Hello World from Async Handler!"); _completed = true; _callback(this); } } } 

I suggest SignalR for bidirectional messages.

+2
source

AHSX is the Go way, but with special regard to files that exceed 4 MB.

What you will need to install Two things:

  • Server side: Ashx HttpHandler, which will process the received data:

    public class FileUpload: IHttpHandler {public void ProcessRequest (HttpContext context) {_httpContext = context;

      if (context.Request.InputStream.Length == 0) throw new ArgumentException("No file input"); try { // Method that will be populating parameters from HttpHandler Request. GetQueryStringParameters(); string uploadFolder = _directory; string tempFileName = _fileName + _tempExtension; if (_firstChunk) { //// Delete temp file if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName)) File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName); //// Delete target file if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _fileName)) File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _fileName); } using (FileStream fs = File.Open(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName, FileMode.Append)) { SaveFile(context.Request.InputStream, fs); fs.Close(); } if (_lastChunk) { File.Move(HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName, HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _fileName); } } catch (Exception e) { // Todo // Logger Exception throw; } } 

    }

  • Silverlight class that will call it:

public class UploadFile {

public void Upload (stream streamNewFile, string fileName, string dirOnServer = "") {this.uploadMode = UploadMode.OpenStream;

  this.destinationOnServer = dirOnServer; this.fileStream = streamNewFile; this.fileName = fileName; this.dataLength = this.fileStream.Length; long dataToSend = this.dataLength - this.dataSent; bool isLastChunk = dataToSend <= this.chunkSize; bool isFirstChunk = this.dataSent == 0; string docType = "document"; var strCurrentHost = HtmlPage.Document.DocumentUri.ToString().Substring(0, HtmlPage.Document.DocumentUri.ToString().LastIndexOf('/')); UriBuilder httpHandlerUrlBuilder = new UriBuilder(strCurrentHost + "/FileUpload.ashx"); httpHandlerUrlBuilder.Query = string.Format("{5}file={0}&offset={1}&last={2}&first={3}&docType={4}&destination={6}", fileName, this.dataSent, isLastChunk, isFirstChunk, docType, string.IsNullOrEmpty(httpHandlerUrlBuilder.Query) ? string.Empty : httpHandlerUrlBuilder.Query.Remove(0, 1) + "&", destinationOnServer); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(httpHandlerUrlBuilder.Uri); webRequest.Method = "POST"; webRequest.BeginGetRequestStream(new AsyncCallback(this.WriteToStreamCallback), webRequest); } 

}

+2
source

All Articles