से फ़ाइल कैसे वापस करें एएसपीनेट 5 में वीपी एपीआई बनाया गया है। मैं पोस्ट अनुरोध के लिए फ़ाइल प्रतिक्रिया वापस करने की कोशिश कर रहा हूं। लेकिन फ़ाइल के बजाय प्रतिक्रिया लग रहा है तरह `एएसपीनेट 5 वेब एपीआई
{
"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
}`
कोड:
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 के बजाय प्रतिक्रिया के रूप में वास्तविक फ़ाइल लौट सकते हैं मैं परीक्षण ग्राहक के रूप में डाकिया उपयोग कर रहा हूँ?।
शायद यह मदद कर सकते हैं: http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api – jpgrassi
nop, यह अभी भी json के रूप में देता है परिणाम: –