【问题标题】:Microsoft Edge ignoring content disposition response header's FileName property?Microsoft Edge 忽略内容处置响应标头的 FileName 属性?
【发布时间】:2016-09-28 08:08:50
【问题描述】:

我正在向浏览器发送文件以保存在本地。这适用于除 Microsoft Edge 之外的所有浏览器,它用 guid 替换文件名。该文件必须使用特定文件名下载,是否有此问题的解释或解决方法?我的回答“不要使用 Edge”将被拒绝。

        var response = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(CreateFile(fileContents))
        };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = string.Concat(fileName, ".xlsx")
        };

【问题讨论】:

标签: c# microsoft-edge httpresponsemessage


【解决方案1】:

我在 Edge 上遇到了同样的问题,当用户点击下载 .ai 文件时,它会自动打开为 .pdf 文件,而不是下载 .ai 文件。

ContentDisposition 是正确的,并且在所有浏览器上都运行良好,但似乎 Edge 出于某种原因忽略了此标头。

经过长时间的调查和尝试,我最终得出了 2 个解决方案:
- 始终将您的文件下载为 Zip 文件(这对我来说不是一个选项,我发现它在移动设备上特别不可接受)
- 将download attribute 添加到下载文件链接并确保属性值具有文件名。 (这解决了边缘问题,并且在所有其他浏览器上都可以正常工作)

更多属性:https://davidwalsh.name/download-attribute

【讨论】:

    猜你喜欢
    • 2014-04-18
    • 1970-01-01
    • 2011-07-13
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2014-08-12
    • 2021-02-25
    相关资源
    最近更新 更多