133

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()?
5

7 Answers 7

199

Better to return HttpResponseMessage with StreamContent inside of it.

Here is example:

public HttpResponseMessage GetFile(string id)
{
    if (String.IsNullOrEmpty(id))
        return Request.CreateResponse(HttpStatusCode.BadRequest);

    string fileName;
    string localFilePath;
    int fileSize;

    localFilePath = getFileFromID(id, out fileName, out fileSize);
       
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = fileName;
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

    return response;
}

UPDATE 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).

10
  • 3
    First thing - this code will cause an exception since you're newing up two FileStream objects pointed at the same file. Second thing is that you do not want to use a "Using" statement, because as soon as the variable goes out of scope, .NET will dispose it and you'll get error messages about the underlying connection being closed. Sep 26, 2012 at 21:44
  • 62
    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).
    – patridge
    Jan 25, 2013 at 21:53
  • 1
    You may also want to override the base dispose() so you can handle your resources correctly when the framework calls it. Jan 16, 2014 at 11:29
  • 1
    This was a very helpful answer, but a user reported not being able to open pdf files returned to the client in this manner using Firefox. The files would open in Firefox as "filename.pdf.htm", which would cause the file not to open properly. Chrome, IE, Edge all worked fine. I found the solution was that you should also set the ContentType like this: response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); Jun 15, 2016 at 16:38
  • 4
    I would like to point out that the correct MediaTypeHeaderValue is crucial and to get it dynamic if you have different file types you can do like this. (where fileName is a string and has a file type ending like .jpg, .pdf, docx etc..) var contentType = MimeMapping.GetMimeMapping(fileName); response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
    – JimiSweden
    Jul 19, 2016 at 11:16
44

I made the follow action:

[HttpGet]
[Route("api/DownloadPdfFile/{id}")]
public HttpResponseMessage DownloadPdfFile(long id)
{
    HttpResponseMessage result = null;
    try
    {
        SQL.File file = db.Files.Where(b => b.ID == id).SingleOrDefault();

        if (file == null)
        {
            result = Request.CreateResponse(HttpStatusCode.Gone);
        }
        else
        {
            // sendo file to client
            byte[] bytes = Convert.FromBase64String(file.pdfBase64);


            result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(bytes);
            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = file.name + ".pdf";
        }

        return result;
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.Gone);
    }
}
3
  • This actually answers the question
    – Mick
    Apr 1, 2016 at 1:23
  • 2
    This would not be a good idea with large files since it loads the entire image into memory. The stream option is better.
    – Paul Reedy
    Feb 28, 2019 at 17:14
  • @PaulReedy Perfect... but in a lot of cases, you don't need to deal with large files. But I totally agree with your point! Oct 8, 2019 at 16:37
42

Just a note for .Net Core: We can use the FileContentResult and set the contentType to application/octet-stream if we want to send the raw bytes. Example:

[HttpGet("{id}")]
public IActionResult GetDocumentBytes(int id)
{
    byte[] byteArray = GetDocumentByteArray(id);
    return new FileContentResult(byteArray, "application/octet-stream");
}
4
  • 7
    This works great, Also if you wanna control the file name there is a property on FileContentResult called FileDownloadName to specify the filename
    – weeksdev
    Jan 13, 2020 at 16:43
  • @weeksdev ah didn't know that. Thanks for the comment. Jan 13, 2020 at 17:50
  • Thats it, thanks. Also comment from weeksdev is very useful.
    – fragg
    Apr 7, 2020 at 13:48
  • Thanks. This actually works for .NET Framework 4.8 as well.
    – sanpat
    Mar 24 at 17:16
22

Example with IHttpActionResult in ApiController.

[HttpGet]
[Route("file/{id}/")]
public IHttpActionResult GetFileForCustomer(int id)
{
    if (id == 0)
      return BadRequest();

    var file = GetFile(id);

    IHttpActionResult response;
    HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.OK);
    responseMsg.Content = new ByteArrayContent(file.SomeData);
    responseMsg.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    responseMsg.Content.Headers.ContentDisposition.FileName = file.FileName;
    responseMsg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    response = ResponseMessage(responseMsg);
    return response;
}

If you don't want to download the PDF and use a browsers built in PDF viewer instead remove the following two lines:

responseMsg.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
responseMsg.Content.Headers.ContentDisposition.FileName = file.FileName;
2
  • @ElbertJohnFelipe Yes, you get the file with var file = GetFile(id);. file.SomeData is a Byte Array (byte[]) and file.FileNameis string.
    – Ogglas
    Feb 12, 2018 at 12:20
  • Thank you for your post. 'HttpResponseMessage' didn't work for me inside an ApiController, so you saved me.
    – Max
    Aug 2, 2018 at 21:38
3

You Can try , HttpClient for Download file from another side and same time you can pass as File Result

 [HttpGet]
    [Route("api/getFile")]
    public async  Task<FileResult> GetFile(string Param1,string Param2)
    {
        try
        {
            Stream stream = null;
            string strURL = @"File URL";
            HttpClient client = new HttpClient();
            HttpResponseMessage httpResponse = await client.GetAsync(strURL);
            Stream streamToReadFrom = await httpResponse.Content.ReadAsStreamAsync();
            return File(streamToReadFrom, "{MIME TYPE}");

        }
        catch (Exception ex)
        {

            throw ex;
        }
        finally
        { 
        
        }
    }
2

I've been wondering if there was a simple way to download a file in a more ... "generic" way. I came up with this.

It's a simple ActionResult that will allow you to download a file from a controller call that returns an IHttpActionResult. The file is stored in the byte[] Content. You can turn it into a stream if needs be.

I used this to return files stored in a database's varbinary column.

    public class FileHttpActionResult : IHttpActionResult
    {
        public HttpRequestMessage Request { get; set; }

        public string FileName { get; set; }
        public string MediaType { get; set; }
        public HttpStatusCode StatusCode { get; set; }

        public byte[] Content { get; set; }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response = new HttpResponseMessage(StatusCode);

            response.StatusCode = StatusCode;
            response.Content = new StreamContent(new MemoryStream(Content));
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = FileName;
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaType);

            return Task.FromResult(response);
        }
    }
1
  • A brief explanation of how your code fixes the OP's problem(s) would enhance the quality of your answer. Mar 2, 2020 at 17:21
1

Another way to download file is to write the stream content to the response's body directly:

[HttpGet("pdfstream/{id}")]
public async Task  GetFile(long id)
{        
    var stream = GetStream(id);
    Response.StatusCode = (int)HttpStatusCode.OK;
    Response.Headers.Add( HeaderNames.ContentDisposition, $"attachment; filename=\"{Guid.NewGuid()}.pdf\"" );
    Response.Headers.Add( HeaderNames.ContentType, "application/pdf"  );            
    await stream.CopyToAsync(Response.Body);
    await Response.Body.FlushAsync();           
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.