1

I've been following the answers in the post linked below in order to return a file using a dotnet api to return a file.

How to return a file using Web API?

In the swagger I am able to make the call however, all I am getting back is a json object instead of the browser opening the file when I navigate directly to the URL.(https://localhost:5001/api/SiteLink/gandrfiles/1).

Any ideas why the browser won't open the file? Thanks!

Json returned:

{
  "version": {
    "major": 1,
    "minor": 1,
    "build": -1,
    "revision": -1,
    "majorRevision": -1,
    "minorRevision": -1
  },
  "content": {
    "headers": [
      {
        "Key": "Content-Disposition",
        "Value": [
          "attachment; filename=Contact_Us_Page.pdf"
        ]
      },
      {
        "Key": "Content-Type",
        "Value": [
          "application/octet-stream"
        ]
      }
    ]
  },
  "statusCode": 200,
  "reasonPhrase": "OK",
  "headers": [],
  "trailingHeaders": [],
  "requestMessage": null,
  "isSuccessStatusCode": true
}

I ended up following the answer on the original post provided by Amir Shirazi and it worked. I don't know why the answer by Regfor didn't do the trick but it didn't work in the project.

durazno33
  • 53
  • 7

1 Answers1

1

You probably need to look at the Content-Type that is being returned from your API. Try "application/octet-steam"

    result.Content.Headers.ContentType = 
        new MediaTypeHeaderValue("application/octet-stream");
AlexDrenea
  • 7,981
  • 1
  • 32
  • 49
  • I modified my code to include that; however following the answer provided by Amir Shirazi on that post, I was able to get it to work. The other ways, just opened a json response but no file. Thanks for the suggestion!. – durazno33 May 12 '20 at 19:22