HTML:
<a href="mysite.com/uploads/asd4a4d5a.pdf" download="foo.pdf">
上传获得唯一的文件名,而真实姓名保存在数据库中。我想实现一个简单的文件下载。但上面的代码重定向到/因为:
$routeProvider.otherwise({
redirectTo: '/',
controller: MainController
});
我尝试过
$scope.download = function(resource){
window.open(resource);
}
但这只是在新窗口中打开文件。
有什么想法可以实现任何文件类型的真正下载吗?
最佳答案
https://docs.angularjs.org/guide/$location#html-link-rewriting
In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.
Links that contain target element Example:
<a href="/ext/link?a=b" target="_self">link</a>
Absolute links that go to a different domain Example:
<a href="http://angularjs.org/">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>
因此,在您的情况下,您应该添加一个目标属性,如下所示...
<a target="_self" href="example.com/uploads/asd4a4d5a.pdf" download="foo.pdf">
关于Angularjs简单文件下载导致路由器重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16670754/