我的问题很简单。我有一个使用Angular构建的接口,如此处所示。当我点击1时,它会打开弹出窗口,然后当我点击2时,它将调用一个函数。请帮我翻译一下。
所使用的模块是ng2-material,其中包含“确认对话框”按钮。
但是我遇到了这个错误:
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,其中包含“确认对话框”按钮。
当单击控制台中显示的链接时,变量url会在控制台中输出一个良好的URLconsole.log(url); =>
,它可以正常下载文件 :)
问题在于下载没有在代码中触发。
编辑1:
移动了一些东西后-
Dashboard.components.ts:
downloadFile(url,file) {
this._Service.getDownloadFile(url,file);
}
在之前的代码中,show download
链接中调用了downloadFile函数。
在_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替换了正确的网址)
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]
有人知道是什么原因导致这个问题吗? 路由模块是否会引起这种情况? 在这个 Plunkr 上它正常运行。
{type: "image/jpeg"}
? - user2180794