Nei*_*mby 5 c# microsoft-edge httpresponsemessage
我正在将文件发送到浏览器以保存在本地。这在除 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")
};
Run Code Online (Sandbox Code Playgroud)
我在 Edge 上遇到了同样的问题,当用户单击下载 .ai 文件时,它会自动打开为 .pdf 文件,而不是下载 .ai 文件。
ContentDisposition 是正确的,并且在所有浏览器上都运行良好,但似乎 Edge 由于某种原因忽略了这个标头。
经过长时间的调查和尝试,我最终得到了 2 个解决方案:
- 始终将文件下载为 Zip 文件(这对我来说不是一个选项,我发现它在移动设备上特别不可接受)
- 将下载属性添加到下载文件链接并确保属性值具有文件名。(这解决了边缘问题并且在所有其他浏览器上运行良好)
有关该属性的更多信息:https://davidwalsh.name/download-attribute
归档时间: |
|
查看次数: |
2039 次 |
最近记录: |