Web api具有此方法;
public HttpResponseMessage GetReport()
{
var data = Logic.GetReportAsByteArray(...);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ByteArrayContent(data);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "filename.pdf";
response.StatusCode = HttpStatusCode.OK;
return response;
}
Web客户端通过此方法进行消费;
...
var request = new HttpRequestMessage(...)
HttpClient client = new HttpClient();
var response = await client.SendAsync(request);
var data = await response.Content.ReadAsByteArrayAsync();
return File(data, "application/pdf", "filename.pdf");
...
当我在本地运行时,我通过Web客户端获得pdf文件作为输出。通过Swagger,我还可以获得文件输出。但是,当我在IIS服务器上进行部署时,我通过Web客户端和Swagger获得了空白的pdf文件,根本没有任何文件,并且响应主体只有下一个文件:
{
"Version": {
"_Major": 1,
"_Minor": 1,
"_Build": -1,
"_Revision": -1
},
"Content": {
"Headers": [
{
"Key": "Content-Type",
"Value": [
"application/pdf"
]
},
{
"Key": "Content-Disposition",
"Value": [
"attachment; filename=filename.pdf"
]
}
]
},
"StatusCode": 200,
"ReasonPhrase": "OK",
"Headers": [],
"RequestMessage": null,
"IsSuccessStatusCode": true
}