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

查看:28
本文介绍了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.

  • 包含目标元素的链接示例:
    link

转到不同域的绝对链接示例:
<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天全站免登陆