C# Web API 2:作为Json序列化的HttpResponseMessage返回的文件响应

C# Web API 2:作为Json序列化的HttpResponseMessage返回的文件响应,c#,.net,asp.net-web-api,asp.net-web-api2,C#,.net,Asp.net Web Api,Asp.net Web Api2,我创建了一个ASP.NET Web API 2控制器POST方法,该方法返回一个包含csv文件的HttpResponseMessage。不幸的是,当我尝试从Angular应用程序使用此方法时,响应总是HttpResponseMessage的序列化版本 控制器方法: [Route("club/{clubId}/audience/export/csv")] [HttpPost] [ProducesResponseType(typeof(HttpResponseMessage), 200)] publ

我创建了一个ASP.NET Web API 2控制器
POST
方法,该方法返回一个包含csv文件的
HttpResponseMessage
。不幸的是,当我尝试从Angular应用程序使用此方法时,响应总是
HttpResponseMessage
的序列化版本

控制器方法:

[Route("club/{clubId}/audience/export/csv")]
[HttpPost]
[ProducesResponseType(typeof(HttpResponseMessage), 200)]
public async Task<HttpResponseMessage> ExportAudienceCSV([FromRoute] int clubId, [FromBody]EmailInput emailInput)
{
   return await this.GenerateCsv($"AudienceExport_{DateTime.UtcNow:HHmmssddMMyy}",
         () => _commsServices.ExportAudienceCSV(clubId, emailInput));
}
请求方法如下所示:

POST http://localhost:51056/api/v1/comms/club/2/audience/export/csv HTTP/1.1
Host: localhost:51056
Connection: keep-alive
Content-Length: 121
Origin: http://localhost:5000
AppTimezoneOffset: 0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
Content-Type: application/json
Accept: application/json, text/plain, */*
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Referer: XXXXXXXXXXXXXXXXXXX
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,en-GB;q=0.8

{"audience":[{"series":378,"divisions":[],"subseries":[],"races":[],"includeSkippers":true,"includeAllBoatAdmins":true}]}
答复如下:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Vary: Origin
Server: Kestrel
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:5000
Access-Control-Expose-Headers: *
Request-Context: appId=cid-v1:73955d83-a87e-4968-b6b4-5878f10f9dab
X-Powered-By: ASP.NET
Date: Thu, 09 Jan 2020 11:17:57 GMT
Content-Length: 416

{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Disposition","value":["attachment; filename=AudienceExport_111755090120.csv"]},{"key":"Content-Type","value":["application/octet-stream"]},{"key":"Content-Length","value":["5915"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"requestMessage":null,"isSuccessStatusCode":true}
如您所见,
HttpResponseMessage
刚刚序列化,但没有包含该文件的内容。知道这里发生了什么吗?

试试下面的代码 首先,您需要在流中创建一个文件

  public HttpResponseMessage GenrateFile(Stream stream, string fileName)
  {
        HttpResponseMessage response;
        response = Request.CreateResponse(HttpStatusCode.OK);
        MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("text/csv");
        response.Content = new StreamContent(stream);
        response.Content = response.Content;
        response.Content.Headers.ContentType = mediaType;
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = fileName;
        return response;
   }

您是否尝试过result.Content.Headers.ContentType=new MediaTypeHeaderValue(“text/csv”)??尝试使用:
返回文件(fileStream,“text/csv”,fileDownloadName)
。你也可以使用Yep,FileResult就是方法。另外,看这个:谢谢,我会试试看。我试过“文本/csv”
  public HttpResponseMessage GenrateFile(Stream stream, string fileName)
  {
        HttpResponseMessage response;
        response = Request.CreateResponse(HttpStatusCode.OK);
        MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("text/csv");
        response.Content = new StreamContent(stream);
        response.Content = response.Content;
        response.Content.Headers.ContentType = mediaType;
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = fileName;
        return response;
   }