G. *_*nos 6 google-chrome .net-core
我正在尝试从我的 MVC 项目(asp.net core 3.1)中获取文件
我创建了一个链接
<a asp-action="@nameof(HomeController.Download)" asp-controller="@HomeController.Name" asp-route-fileName="fileName.doc" download>FileName</a>
Run Code Online (Sandbox Code Playgroud)
我创建了一个控制器
public async Task<ActionResult> Download(string fileName) {
var path = Path.Combine(_hostingEnvironment.WebRootPath, fileName);
if (!System.IO.File.Exists(path)) {
return NotFound();
}
var fileBytes = await System.IO.File.ReadAllBytesAsync(path);
var response = new FileContentResult(fileBytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
FileDownloadName = fileName
};
return response;
}
Run Code Online (Sandbox Code Playgroud)
在 Chrome 中我收到警告
不允许下载。启动或实例化下载的帧被沙盒化,但未设置标志“允许下载”。有关更多详细信息,请参阅https://www.chromestatus.com/feature/5706745674465280。
按照链接,我如何添加:
将“允许下载”添加到沙箱属性列表以选择加入
如果我单击 Microsoft Edge 中的按钮,则会下载该文件
这就是我必须为我的项目做的事情,希望它能为您指明正确的方向。
在你的 Startup.cs 中
services.AddMvc(options =>{
options.Filters.Add(new MyActionFilterAttribute());
}
Run Code Online (Sandbox Code Playgroud)
然后在MyActionFilterAttribute.cs中
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Headers.Add("Content-Security-Policy", "sandbox allow-downloads; " )
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1817 次 |
| 最近记录: |