我从数据库中得到一个bytearray:
byte[] bytDocu;
我想通过API将此bytearray作为File返回:
[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);
}
}
如何将Bytearray转换为文件? 我如何将它交给Ok()函数?
感谢您的帮助:)
答案 0 :(得分:3)
使用下面的代码
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;