1
我有這樣的異步處理ASP.NET異步處理內容類型不發送
public sealed class ImageTransferHandler : IHttpAsyncHandler
{
public bool IsReusable { get { return false; } }
public ImageTransferHandler()
{
}
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
{
string url = context.Server.UrlDecode(context.Request.QueryString["url"]);
ImageTransferOperation ito = new ImageTransferOperation(cb, context, extraData);
ito.Start(url);
return ito;
}
public void EndProcessRequest(IAsyncResult result)
{
}
public void ProcessRequest(HttpContext context)
{
throw new InvalidOperationException();
}
private class ImageTransferOperation : IAsyncResult
{
private Object state;
private bool isCompleted;
private AsyncCallback cb;
private HttpContext context;
public WaitHandle AsyncWaitHandle
{
get { return null; }
}
public bool CompletedSynchronously
{
get { return false; }
}
public bool IsCompleted
{
get { return isCompleted; }
}
public Object AsyncState
{
get { return state; }
}
public ImageTransferOperation(AsyncCallback cb, HttpContext context, Object state)
{
this.cb = cb;
this.context = context;
this.state = state;
this.isCompleted = false;
}
public void Start(string url)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartTransfer), url);
}
private void StartTransfer(Object state)
{
string url = (string)state;
System.Net.WebClient wc = new System.Net.WebClient();
byte[] bytes = wc.DownloadData(url);
context.Response.Headers.Add("Content-Type", "image/jpeg");
context.Response.Headers.Add("Content-Length", bytes.Length.ToString());
context.Response.BinaryWrite(bytes);
isCompleted = true;
cb(this);
}
}
}
一切正常,只是「內容類型」標頭不發送。 我試圖用
context.Response.Headers.Add("Content-Type", "image/jpeg");
和
context.Response.Headers["Content-Type"] = "image/jpeg";
,該怎麼辦錯把它兩者兼而有之?
試過Response.ContentType? – Nathan 2011-06-06 11:09:02
太棒了!感謝名單。我不知道這個頭是異常的。非常直觀的控制。 – aikixd 2011-06-06 11:12:43
沒問題 - 看到工作,請接受答案,下面添加:) – Nathan 2011-06-07 07:17:53