我所做的是由 Silverlight 中的 Handlers.(ASHX) 同步的。RadFileUploader 是 Telerik 的组件与这些处理程序一起使用。
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);
}
}
}
我削减了大部分代码,但它给出了这个想法。我使用了一个处理程序来播放音频文件和一个处理程序来下载音频文件。Mimetype 是非常重要的部分。你的标题也很重要。通过这种方式,你告诉你想做什么。
对于异步,此页面可能会有所帮助。http://msdn.microsoft.com/en-us/library/ms227433(v=vs.100).aspx
编辑
下面的处理程序文件"Top.jasper"
从目录中读取文件并将其写入回调方法。(ar.IsCompleted)
参数的 IsCompleted 属性检查其是否已完成。
<%@ 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);
}
}
}
我提供SignalR
双向通信。