gpt4 book ai didi

angular - 在 TypeScript (Angular) 中打开下载链接

转载 作者:太空狗 更新时间:2023-10-29 18:34:19 38 4
gpt4 key购买 nike

我的问题很简单。我有 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 在控制台中给了我一个很好的 url console.log(url); => 当我点击控制台中显示的链接时,它会很好地下载文件 :)

问题是下载没有在代码中触发。

编辑 1:

搬了一些东西之后-

仪表板.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它运行良好。

最佳答案

一种方法是通过 AJAX 请求加载图像,然后利用 FileSaver库打开下载对话框。

这是一个例子。您需要扩展 BrowserXhr 类来设置响应类型。

@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
constructor() {}
build(): any {
let xhr = super.build();
xhr.responseType = "blob";
return <any>(xhr);
}
}

然后就可以利用这个类来执行获取图片内容的请求了:

@Component({
selector: 'download',
template: `
<div (click)="downloadFile() ">Download</div>
`,
providers: [
provide(CustomBrowserXhr,
{ useClass: CustomBrowserXhr }
]
})
export class DownloadComponent {
@Input()
filename:string;

constructor(private http:Http) {
}

downloadFile() {
this.http.get(
'https://mapapi.apispark.net/v1/images/'+this.filename).subscribe(
(response) => {
var mediaType = 'application/pdf';
var blob = new Blob([response._body], {type: mediaType});
var filename = 'test.pdf';
saveAs(blob, filename);
});
}
}

这个覆盖(设置响应类型)将只适用于这个组件(不要忘记在引导你的应用程序时删除相应的提供)因为我在 providers 属性中设置了提供者成分。下载组件可以这样使用:

@Component({
selector: 'somecomponent',
template: `
<download filename="'Granizo.pdf'"></download>
`
, directives: [ DownloadComponent ]
})

有关详细信息,请参阅此答案:

关于angular - 在 TypeScript (Angular) 中打开下载链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568082/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com