【发布时间】:2016-04-12 08:49:12
【问题描述】:
我的问题很简单。我有 this interface 用 Angular 构建的。当我点击 1 时,它会打开弹出窗口,然后当我点击 2 时,它会调用一个函数:
showDownloadLink(ev, name) {
let config = new MdDialogConfig()
.textContent('<a href="http://www.google.fr">lol</a>')
.clickOutsideToClose(false)
.title('Would you like to delete your debt?')
.ariaLabel('Lucky day')
.ok('Please do it!')
.cancel('Sounds like a scam')
.targetEvent(ev);
this.dialog.open(MdDialogBasic, this.element, config)
.then((ref: MdDialogRef) => {
ref.whenClosed.then((result) => {
if (result) {
var url = this._Service.getDownloadLink(name);
console.log(url);
//window.open(url,'Download');
//this.downloadURI(url );
}
else {
this.status = 'You decided to keep your debt.';
}
})
});
}
downloadURI(uri) {
var link = document.createElement("a");
link.href = uri;
link.click();
}
使用的模块是ng2-material,带有“确认对话框”按钮。
当我点击控制台中显示的链接时,var url 在控制台console.log(url); => 中为我提供了一个很好的 url,它可以很好地下载文件:)
问题是代码中没有触发下载。
编辑 1:
移动一些东西后-
Dashboard.components.ts:
downloadFile(url,file) {
this._Service.getDownloadFile(url,file);
}
downloadFile 像以前一样在show download 链接中调用。
在_service.ts中:
import {Injectable} from "angular2/core";
import {S3Object} from "./s3object";
import {Observable} from "rxjs/Observable";
import {Http, Headers, Response} from "angular2/http";
import "rxjs/Rx";
import {CredService} from "./cred.service";
//mettre les parenthèses !!
@Injectable()
export class Service {
private serviceUrl = 'url';
private token;
private headers = new Headers();
private credS;
constructor(private http:Http, private cred:CredService) {
this.credS = cred;
this.token = cred.getToken();
this.headers.append('Authorization', 'bearer ' + this.token);
this.headers.append('Content-Type', 'application/json');
}
getDownloadLink(path:string){
var opUrl = 'download.json?objectKey='+path;
var urlRes = "";
this.http.get(
this.serviceUrl + opUrl,
{ headers: this.headers }
)
.map(res => res.text())
.subscribe(
data => urlRes = data,
err => console.log(err),
() => console.log(urlRes)
);
//console.log(urlRes);
return urlRes;
}
getDownloadFile(url:string, file:string) {
this.http.get(url).subscribe(
(response) => {
var blob = new Blob([response._body], {type: "application/text"});
var filename = file;
saveAs(blob, filename);
});
}
}
还有一个 customBrowserXhr.ts 文件:
import {Injectable} from 'angular2/core';
import {BrowserXhr} from 'angular2/http';
@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
constructor() {}
build(): any {
let xhr = super.build();
xhr.responseType = "blob";
console.log("it's cool bro ! ");
return <any>(xhr);
}
}
编辑2:
我的控制台函数执行日志(我在代码中用URL替换了正确的url)
test url ddl before : "URL/Desert.jpg?serieofparams"
test name ddl before : Desert.jpg
功能:
downloadFile(url,file) {
console.log('test url ddl before : ' + url);
console.log('test name ddl before : ' + file);
this.http.get(url).subscribe(
(response) => {
var blob = new Blob([response._body], {type: "image/jpeg"});
var filename = file;
saveAs(blob, filename);
});
}
但是我收到了这个错误:
Failed to load resource: the server responded with a status of 404 (Introuvable) http://localhost:8080/context/%22URL/Desert.jpg?serieofparams%22
angular2.dev.js:23740 EXCEPTION: [object Object]
有人知道是什么原因造成的吗? 路由模块有可能这样做吗? 在 this Plunkr 上,它运行良好。
【问题讨论】:
-
我怎样才能在下载中允许多种类型.. 比如 pdf、lpg、png
{type: "image/jpeg"});
标签: angular