【发布时间】:2018-09-04 11:10:52
【问题描述】:
我正在尝试以角度 4 从 UI 下载图像。到目前为止,我已应用以下代码下载图像:
DownloadImageFromUrl(imageUrl) {
imageUrl = this.ImagePath + imageUrl;
this.Service.GetImage(imageUrl, { responseType: ResponseContentType.Blob })
.map(res => {
return new Blob([res._body], {
type: res.headers.get("Content-Type")
});
})
.map(blob => {
var urlCreator = window.URL;
return
this.sanitizer.bypassSecurityTrustUrl(urlCreator.createObjectURL(blob));
})
}
在“GetImage”函数之后:
GetImage(url: string, options?: RequestOptionsArgs): Observable<any> {
return this._http.get(url, this.addWithImage(options)).map((res) => res.json());
}
private addWithImage(options?: RequestOptionsArgs): RequestOptionsArgs {
// ensure request options and headers are not null
options = options || new RequestOptions();
options.headers = options.headers || new Headers();
// add authorization header with token
const token = 'XXXXXXXX';
if (token) {
options.headers.append('Authorization', 'Bearer ' + token);
options.headers.append('Content-Type', 'image/jpg');
}
return options;
}
请告诉我这段代码有什么问题?
【问题讨论】: