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.
source share