My previous question: How to return file from ASP.net 5 web api
I am trying to return a file as the response from Web API POST request.
I'm using dnx451 framework and rc1-final build. Controller method:
[HttpPost("")]
public ActionResult 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));
FileStream stream = new FileStream(file,FileMode.Open);
return File(stream, "application/pdf", "test.pdf");
}
}
catch (Exception ex)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return null;
}
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return null;
}
As result I get a file with name "response". After saving it as pdf i try to open it, and it says it is damaged. Hope you can help me. I am using Postman as test client.
Thanks
return File()line? What is the content and headers received in the response?Helper.GeneratePdf()line doing its work correctly? What is the value offile? Can the file be opened, and did you inspected the file being generated before sending it back to the browser?