有以下方法
[Route("{id:int}/attachment"), HttpGet]
public HttpResponseMessage GetAttachment([FromUri]int id)
{
var result = Request.CreateResponse(HttpStatusCode.OK);
var fileName = "Имя файла";
var mimeType = MimeMapping.GetMimeMapping(fileName);
try
{
var path = "Путь к файлу";
result.Content = new ByteArrayContent(File.ReadAllBytes(path));
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{ FileName = HttpUtility.UrlPathEncode(fileName) };
result.Content.Headers.Add("Content-Type", mimeType);
return result;
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
}
}
文件下载是通过向对应的webapi端点发送ajax请求发起的,在开发者控制台发送请求一段时间后,请求获取到状态 (failed )net::ERR_CONNECTION_RESET,在系统日志中发现以下错误操作系统
ArithmeticException 算术运算中溢出或精度丢失。
http://localhost:58863/api/operations/22461/items/226512/attachment
事实证明,只有大文件才会出现此问题,在本例中文件大小为 1.4GB。
如何下载大文件