我有一些存储在数据库中的文件需要通过Web API返回。数据库调用可以正确地返回正确的字节数组(断点显示数组长度约为67000,这是正确的),但是当我调用Web API时,响应中从未返回该内容。我尝试使用MemoryStream和ByteArrayContent,但都没有给出我应该得到的结果。我尝试从Postman和我的MVC应用程序中调用,但都没有返回字节数组,只返回基本的响应信息和头部/成功等。
public HttpResponseMessage GetFile(int id)
{
var fileToDownload = getFileFromDatabase(id);
if (fileToDownload == null)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(fileToDownload.FileData); //FileData is just a byte[] property in this class
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
}
我通常收到的响应(没有任何字节内容可找到):
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Disposition",
"value": [
"attachment"
]
},
{
"key": "Content-Type",
"value": [
"application/octet-stream"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}
也许我对如何处理这些数据存在误解,但我认为由于我明确添加了它,所以应该从Web API调用中返回它。