MVC Web Api 2:如何允许并行下载文件
本文关键字:何允许 并行 下载 文件 Web Api MVC | 更新日期: 2023-09-27 18:21:15
下面是我的代码,它与下载zip文件有关。所有事情都很好,但当有一个正在进行的下载请求时,它无法下载文件
public class ZipController : ApiController
{
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public HttpResponseMessage DownloadFile()
{
var zipPath = ConfigurationManager.AppSettings["FilePath"];
var result = new HttpResponseMessage(HttpStatusCode.OK);
try
{
if (File.Exists(zipPath))
{
var stream = new FileStream(zipPath, FileMode.Open);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "abc.zip"
};
}
}
catch (Exception ex)
{
LogError.LogErrorToFile(ex);
}
return result;
}
}
当我提供文件访问权限时,它就起作用了。
public class ZipController : ApiController
{
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public HttpResponseMessage DownloadFile()
{
var zipPath = ConfigurationManager.AppSettings["FilePath"];
var result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(zipPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
try
{
if (File.Exists(zipPath))
{
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "abc.zip"
};
}
}
catch (Exception ex)
{
LogError.LogErrorToFile(ex);
}
return result;
}
}