简体   繁体   English

返回HttpResponseMessage的控制器未正确设置内容类型

[英]Controller returning HttpResponseMessage not setting content type correctly

I have the following code in a controller to return a stream which is an Excel File to download on the client side: 我在控制器中有以下代码来返回流,该流是要在客户端下载的Excel文件:

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
MemoryStream memStream = ReportExporter.ExportReport(analystReport);

response.Content = new StreamContent(memStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = saveAsFileName;

return response;

When I make the request on the client side it simply gives me back the wrapper of the response message: 当我在客户端发出请求时,它只是将响应消息的包装返回给我:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Content-Type: application/octet-stream
  Content-Disposition: attachment; filename=.xlsx
}

I believe this maybe due to the headers coming back as text as this is what I see in Chrome: 我相信这可能是由于标题以文本形式返回的原因,这就是我在Chrome中看到的内容:

Content-Type:text/html; charset=utf-8   

I can't work out what is happening here having tried various things. 我尝试了各种各样的事情后无法弄清楚这里发生了什么。

Try this solution: 试试这个解决方案:

// Copy file to byte array
byte[] file = new byte[memStream.Length];
memStream.Read(file, 0, file.Length);

// Send file
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "inline;filename=" + saveAsFileName);
Response.Buffer = true;
Response.Clear();
Response.BinaryWrite(file);
Response.End();

// Return success
return new HttpStatusCodeResult(HttpStatusCode.OK);

Response is a Controller property, so you don't need to create any HttpResponseMessage object. ResponseController属性,因此不需要创建任何HttpResponseMessage对象。

There are many ways to solve this, but I will show you mine. 有很多方法可以解决此问题,但我将向您展示。 Instead of HttpResponseMessage you can just do FileStreamResult. 除了HttpResponseMessage,您还可以执行FileStreamResult。 And You dont need to instantiate HttpResponseMessage, its already part of the controller method. 而且您不需要实例化HttpResponseMessage,它已经是控制器方法的一部分。 This is how I would start to try to fix your code. 这就是我开始尝试修复您的代码的方式。 Remember to use FileStreamResult instead. 记住要改用FileStreamResult。

    public FileStreamResult DownloadExcel(???analystReport????)
    {
        MemoryStream memStream = ReportExporter.ExportReport(analystReport);

        return new FileStreamResult(memStream , "application/vnd.ms-excel", saveAsFileName);
    }

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

相关问题 HttpResponseMessage 内容未返回正确的内容类型 - HttpResponseMessage Content not returning the correct content type WCF Restful返回HttpResponseMessage想要在设置内容时进行协商 - WCF Restful returning HttpResponseMessage wants to negotiate when setting content Content-Type 字符集是否未从 HttpResponseMessage 公开? - Is the Content-Type charset not exposed from HttpResponseMessage? HttpResponseMessage针对内容类型image / jpeg调用了两次 - HttpResponseMessage called twice for content type image/jpeg 基于HttpResponseMessage中内容类型的Web Api文档 - Web Api documentation based on type of the content in HttpResponseMessage 将HttpResponseMessage内容设置为字符串和JSON文件之间是否有性能差异? - Is there any performance difference between setting HttpResponseMessage content as string and as JSON file? 如何从 HttpResponseMessage 读取应用程序/pdf 内容类型并将其转换为 stream - How to read application/pdf content type from HttpResponseMessage and convert it into stream 无法在 HttpResponseMessage 标头上设置 Content-Type 标头? - Can't set Content-Type header on HttpResponseMessage headers? 返回HTTP 403.7 HTTPResponseMessage - Returning an HTTP 403.7 HTTPResponseMessage GetAsync:不返回HttpResponseMessage - GetAsync : not returning HttpResponseMessage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM