【发布时间】:2019-05-17 10:19:39
【问题描述】:
这是我的行动...
[HttpGet]
public object Download()
{
return File("file.png", MediaTypeNames.Application.Octet, "HiMum.png");
}
在浏览器中...
axios({
url: AppPath + 'download/',
method: 'GET',
responseType: 'blob'
})
.then(response => {
console.log(JSON.stringify(response.headers));
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
link.click();
window.URL.revokeObjectURL(url);
});
跟踪只显示.. .
{"content-type":"application/octet-stream"}
如何获取文件名?我已经尝试添加...
Response.Headers.Add("Content-Disposition", $"inline; filename=HiMum.png");
.. 在行动中。
【问题讨论】:
-
在您的 JS 中,您应该能够访问
response变量上的标题。访问Content-Disposition标头,并从中解析出文件名。 -
@ChrisPratt 代码已经尝试过这个,当直接指定“Content-Disposition”时它是未定义的。
-
嗨,有趣,这可能有一些额外的信息stackoverflow.com/posts/25715985/revisions
-
未定义是什么意思?
-
在 Javascript 中,undefined 是当你访问一个不存在的变量时得到的值。
标签: asp.net-core-mvc axios asp.net-core-webapi