Angularjs简单的文件下载 [英] Angularjs simple file download

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

问题描述

HTML

<a href="mysite.com/uploads/asd4a4d5a.pdf" download="foo.pdf">

上传得同时有真实姓名保存在数据库中的唯一的文件名。我想实现一个简单的文件下载。但上面的code重定向到/因为:

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?

推荐答案

<一个href=\"https://docs.angularjs.org/guide/$location#html-link-rewriting\">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.


      
  • 这包含目标元素实例链接:结果
      &LT; A HREF =/转/链路A = B?TARGET =_自我&GT;链接&LT; / A&GT;

绝对链接,去到不同的域实例:结果
  &LT; A HREF =htt​​p://angularjs.org/&GT;链接&LT; / A&GT;

Absolute links that go to a different domain Example:
<a href="http://angularjs.org/">link</a>

以'/'链接,导致不同的基本路径时基地定义示例:搜索
  &LT; A HREF =/不是我的基地/链接&GT;链接&LT; / A&GT;

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天全站免登陆