繁体   English   中英

ASP.NET 异步处理程序内容类型未发送

[英]ASP.NET async handler content-type not sent

我有这个异步处理程序

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);
        }
    }
}

一切正常,除了“内容类型”header 未发送。 我试着把它都用

context.Response.Headers.Add("Content-Type", "image/jpeg");

context.Response.Headers["Content-Type"] = "image/jpeg";

我做错了什么?

使用 Response.ContentType。 很高兴这对你有用:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM