问题描述
HTML:
<a href="mysite.com/uploads/asd4a4d5a.pdf" download="foo.pdf">
当数据库中保留真实姓名时,上传文件将获得唯一的文件名.我想实现一个简单的文件下载.但由于以下原因,上面的代码重定向到/:
Uploads get a unique file name while there real name is kept in database. I want to realize a simple file download. But the code above redirects to / because of:
$routeProvider.otherwise({
redirectTo: '/',
controller: MainController
});
我尝试过
$scope.download = function(resource){
window.open(resource);
}
但这只是在新窗口中打开文件.
but this just opens the file in a new window.
有什么想法可以为任何文件类型启用真正的下载吗?
Any ideas how to enable a real download for any file type?
推荐答案
https://docs. angularjs.org/guide/$location#html-link-rewriting
-
包含目标元素的链接示例:
<a href="/ext/link?a=b" target="_self">link</a>
Links that contain target element Example:
<a href="/ext/link?a=b" target="_self">link</a>
转到其他域的绝对链接示例:
<a href="http://angularjs.org/">link</a>
Absolute links that go to a different domain Example:
<a href="http://angularjs.org/">link</a>
以'/'开头的链接在定义base时导致不同的基本路径示例:
<a href="/not-my-base/link">link</a>
Links starting with '/' that lead to a different base path when base is defined Example:
<a href="/not-my-base/link">link</a>
因此,在您的情况下,应添加目标属性,例如...
So in your case, you should add a target attribute like so...
<a target="_self" href="example.com/uploads/asd4a4d5a.pdf" download="foo.pdf">
这篇关于AngularJS简单文件下载导致路由器重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!