I am using ASP.NET Web API.
I want to download a PDF with C# from the API (that the API generates).
Can I just have the API return a byte[]
? and for the C# application can I just do:
byte[] pdf = client.DownloadData("urlToAPI");?
and
File.WriteAllBytes()?
Just a note for
.Net Core
: We can use theFileContentResult
and set the contentType toapplication/octet-stream
if we want to send the raw bytes. Example:Example with
IHttpActionResult
inApiController
.If you don't want to download the PDF and use a browsers built in PDF viewer instead remove the following two lines:
Better to return HttpResponseMessage with StreamContent inside of it.
Here is example:
UPD from comment by patridge: Should anyone else get here looking to send out a response from a byte array instead of an actual file, you're going to want to use new ByteArrayContent(someData) instead of StreamContent (see here).
I made the follow action: