【问题标题】:How to add "allow-downloads" to the sandbox attributes list如何将“允许下载”添加到沙盒属性列表
【发布时间】:2020-06-01 07:36:27
【问题描述】:

我正在尝试从我的 MVC 项目 (asp.net core 3.1) 中获取文件

我创建了一个链接

<a asp-action="@nameof(HomeController.Download)" asp-controller="@HomeController.Name" asp-route-fileName="fileName.doc" download>FileName</a>

我创建了一个控制器

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;
        }

在 Chrome 中我收到警告

不允许下载。启动或实例化下载的框架是沙盒的,但未设置标志“allow-downloads”。详情请见https://www.chromestatus.com/feature/5706745674465280

按照链接,我该如何添加:

将“允许下载”添加到沙盒属性列表以选择加入

如果我单击 Microsoft Edge 中的按钮,则会下载文件

【问题讨论】:

    标签: google-chrome .net-core


    【解决方案1】:

    这是我必须为我的项目做的事情,希望它能为您指明正确的方向。

    在你的 Startup.cs 中

    services.AddMvc(options =>{
       options.Filters.Add(new MyActionFilterAttribute());
    }
    

    然后在 MyActionFilterAttribute.cs 中

     public class MyActionFilterAttribute : ActionFilterAttribute
            {
                public override void OnResultExecuting(ResultExecutingContext filterContext)
                {
                    filterContext.HttpContext.Response.Headers.Add("Content-Security-Policy",  "sandbox allow-downloads; " )
                }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      相关资源
      最近更新 更多