- List item
用于文件下载api 将文件流设置读取文件模式,否则高并发下载相同文件时会下载失败
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage DownloadUpdateZip(string subpath)
{
string updateZipPath = HostingEnvironment.MapPath("~/UploadFiles/" + subpath);
if (!File.Exists(updateZipPath))
{
return new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent("请求下载的文件不存在") };
}
if (Path.GetExtension(updateZipPath).ToLower() != ".zip")
{
return new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent("请求下载错误") };
}
string fileName = Path.GetFileName(updateZipPath);
FileStream stream = new FileStream(updateZipPath, FileMode.Open,FileAccess.Read);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = HttpUtility.UrlEncode(fileName)
};
response.Headers.Add("Access-Control-Expose-Headers", "FileName");
response.Headers.Add("FileName", HttpUtility.UrlEncode(fileName));
return response;
}