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

发布于 2024-11-14 11:00:12 字数 2411 浏览 2 评论 0原文

我有这个异步处理程序,

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

除了未发送“Content-Type”标头之外,一切正常。 我尝试同时发送

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

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

我做错了什么?

I have this async handler

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

Everything works except that the "Content-Type" header not sent.
I tried to send it both with

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

and

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

What do I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-11-21 11:00:12

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

Use Response.ContentType. Glad that worked for you :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文