AngularJS简单文件下载导致路由器重定向 [英] Angularjs simple file download causes router to redirect

查看:55
本文介绍了AngularJS简单文件下载导致路由器重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在以下情况下,链接不会被重写;相反, 浏览器将执行整页重新加载到原始链接.

In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.

  • 包含目标元素的链接示例:
    <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简单文件下载导致路由器重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆