Reputation: 23
The below code is returning JSON object instead of downloading the PDF file:
var dataStream = new MemoryStream(System.IO.File.ReadAllBytes(@"C:\data\test12.pdf"));
dataStream.Position = 0;
HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.OK);
responseMsg.Content = new StreamContent(dataStream);
responseMsg.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
responseMsg.Content.Headers.ContentDisposition.FileName = file.Rows[0][1].ToString();
responseMsg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
responseMsg.Content.Headers.ContentLength = dataStream.Length;
result = responseMsg;
output:
{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Disposition","value":["inline; filename=4159a9f5-0102-46c3-a39f-7449c00e39e5.pdf"]},{"key":"Content-Type","value":["application/pdf"]},{"key":"Content-Length","value":["7874"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"requestMessage":null,"isSuccessStatusCode":true}
Upvotes: 1
Views: 813
Reputation: 36575
The below code is returning JSON object instead of downloading the pdf file.
This is the expected result when you call this api. In your current code the framework is treating HttpResponseMessage
as a model.
The correct way to download a file after calling the api should be:
public IActionResult Get()
{
var dataStream = new MemoryStream(System.IO.File.ReadAllBytes(@"C:\data\test12.pdf"));
Response.Headers.Add("Content-Disposition", "inline; filename=test.pdf");
return File(dataStream, "application/pdf");
}
If you want to use HttpResponseMessage
, you need call the api by ajax.
Reference: https://stackoverflow.com/a/41115583/11398810
Upvotes: 1