1

I get a bytearray from the database:

byte[] bytDocu;

I want to return this bytearray as File via an API:

[HttpGet]
[Route("GetFile/{key}")]
public IHttpActionResult GetFile(int key)
{

    try
    {
        byte[] bytDocu = _documentService.getFile(key);
        return Ok(result);
    }
    catch (Exception e)
    {
        return BadRequest(e.Message);
    }
}

How do I turn the Bytearray into a File? And how do I hand it to the Ok()-function?

Thanks for your help :)

NDDT
  • 445
  • 1
  • 10
  • 27

1 Answers1

3

use below code

localFilePath = "file path";
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(filepath, 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 or set your content type");

return response;
Koderzzzz
  • 849
  • 8
  • 18