asp.netでwep apiを作成しました。5.私はPost要求に対してファイル応答を返しています。ASP.netからファイルを返す方法5 web api
public HttpResponseMessage Post([FromBody]DocumentViewModel vm)
{
try
{
if (ModelState.IsValid)
{
var Document = _repository.GetDocumentByGuid(vm.DocumentGuid, User.Identity.Name);
var Params = Helper.ClientInputToRealValues(vm.Parameters, Document.DataFields);
var file = Helper.GeneratePdf(Helper.InsertValues(Params, Document.Content));
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(System.IO.File.ReadAllBytes(file))
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "test.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
}
catch (Exception ex)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return null;
}
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return null;
}
は、どのように私の代わりにJSONの応答として、実際のファイルを返すことができ、私はテストクライアントとしてポストマンを使用しています:?しかし、その代わりに、ファイルの応答は、コード `
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Disposition",
"value": [
"attachment; filename=test.pdf"
]
},
{
"key": "Content-Type",
"value": [
"application/pdf"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}`
のように見えます。
おそらくこれは助けることができる:http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api – jpgrassi
NOP、それはまだのようにJSONを返します結果: –