Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return file from ASP.net 5 web api

Created wep api in asp.net 5. I am tring to return file response for Post request. But instead of file the response looks like `

{
  "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
}`

Code:

    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;

    }

How can I return the real file as the response instead of JSON ?I am using Postman as test client.

like image 502
Joonas Püüa Avatar asked Jan 18 '16 11:01

Joonas Püüa


2 Answers

used IActionResult instead of HttpResponseMessage. And returned FileStreamResult, and got it working.

Got a new problem, the file is not the one I open with the stream from server. But will create a new question for that.

Continues : Return file from ASP.NET 5 Web API

Thanks

like image 115
Joonas Püüa Avatar answered Nov 18 '22 13:11

Joonas Püüa


This is the "low-level" HTTP approach, which should work with both ASP.NET WebAPI or ASP.NET MVC.

[HttpGet]
public HttpResponseMessage Download()
{
  var fs = new FileStream(myfileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 32768, true);
  var response = new HttpResponseMessage {
    Content = new StreamContent(fs);
  }
  response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
  response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
  return response;
}
like image 34
MoSad Avatar answered Nov 18 '22 14:11

MoSad