简体   繁体   English

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

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

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.一切正常,除了“内容类型”header 未发送。 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?我做错了什么?

Use Response.ContentType.使用 Response.ContentType。 Glad that worked for you:)很高兴这对你有用:)

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

相关问题 ASP.NET MVC应用程序不输出内容类型标头 - ASP.NET MVC Application not outputting content-type header 在ASP.NET MVC中设置空响应的Content-Type - Setting the Content-Type of an empty response in ASP.NET MVC 使用合适的内容类型将图像上传到ASP.NET网站 - Uploading image to ASP.NET site with avalible content-type ASP.Net SoapHttpClientProtocol中的HTTP内容类型 - HTTP Content-Type in ASP.Net SoapHttpClientProtocol InvalidOperationException:错误的内容类型:ASP.NET Core - InvalidOperationException: Incorrect Content-Type: ASP.NET Core 尝试使用Ajax将png上载到Asp.net Core服务器时出现“错误的Content-Type:image / png” - “Incorrect Content-Type: image/png” when trying to upload png with Ajax to Asp.net Core server 发送到ASP.NET Web API时,请求标头中Angular 5缺少Content-type - Angular 5 missing Content-type in request headers when send to ASP.NET Web API 如何从ASP.NET Core MVC响应的Content-Type中删除字符集? - How do I remove the charset from Content-Type in a ASP.NET Core MVC response? ASP.NET重写的自定义错误不会发送内容类型标头 - ASP.NET rewritten custom errors do not send content-type header 当我使用ASP.NET传输文件时,为什么IIS7会忽略我的内容类型标头? - Why does IIS7 ignore my content-type header when I use ASP.NET to stream files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM