我有一个通过URL上传图片的功能。 我想更改文件名,下载文件:' image.jpg'。
这就是我的所作所为:
public downloadImageJpeg(instanceUID: string, format: string): string {
var a = document.createElement("a");
a.download = 'image.jpg';
a.href = this.getRootUrl() + `/dicom/instances/${instanceUID}/wado/jpg`;
var e = document.createEvent("MouseEvents");
e.initMouseEvent("click", true, false, document.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
我的图片已上传,但其名称未更改,而且是' jpg.jpg'。你知道为什么它不是' image.jpg' ??
////////////// EDIT //////////////:
但是,我认为我的错误来自a.href
。在一些例子中,我看到他被给了参数。
我试过这个:
a.href = "data:image/jpg;base64," + this.getRootUrl() + `/dicom/instances/${instanceUID}/wado/jpg`;
文件名更改但我没有下载,这让我发现网络故障 - 错误'
答案 0 :(得分:0)
以下功能正常:
export function download(url: string, filename: string) {
var a = document.createElement('a');
if (a.click) {
// Use a.click() if available. Otherwise, Chrome might show
// "Unsafe JavaScript attempt to initiate a navigation change
// for frame with URL" and not open the PDF at all.
// Supported by (not mentioned = untested):
// - Firefox 6 - 19 (4- does not support a.click, 5 ignores a.click)
// - Chrome 19 - 26 (18- does not support a.click)
// - Opera 9 - 12.15
// - Internet Explorer 6 - 10
// - Safari 6 (5.1- does not support a.click)
a.href = url;
a.target = '_parent';
// Use a.download if available. This increases the likelihood that
// the file is downloaded instead of opened by another PDF plugin.
if ('download' in a) {
a.download = filename;
}
// <a> must be in the document for IE and recent Firefox versions.
// (otherwise .click() is ignored)
(document.body || document.documentElement).appendChild(a);
a.click();
a.parentNode!.removeChild(a);
} else {
if (window.top === window &&
url.split('#')[0] === window.location.href.split('#')[0]) {
// If _parent == self, then opening an identical URL with different
// location hash will only cause a navigation, not a download.
var padCharacter = url.indexOf('?') === -1 ? '?' : '&';
url = url.replace(/#|$/, padCharacter + '$&');
}
window.open(url, '_parent');
}
}