【发布时间】:2021-05-19 05:43:49
【问题描述】:
当我点击文件名时,它将成功下载文件(d7f1eb91-6a65-4fde-8151-d9fb6afb7cfb_Desert.jpg)。但我希望下载为 Desert.jpg。您能帮忙找出问题吗?
HTML:
<p *ngIf="removebtn" (click)="showPdf(employeeDetails.certificateurl)" class="wordBreak"><a style="cursor: pointer;" target="_blank">{{(employeeDetails.showcertificateurl)}}</a>
<i class="fa fa-times" style="color:red; cursor: pointer;font-size: 20px; margin-left: 10px;" aria-hidden="true" (click)="removeImage()">
</i>
</p>
组件:
showPdf(file) {
console.log(file)
//https://testkimage.s3.us-east-2.amazonaws.com/upload/image/d7f1eb91-6a65-4fde-8151-d9fb6afb7cfb_Desert.jpg
const linkSource = file;
const downloadLink = document.createElement("a");
let fileName = "";
downloadLink.href = linkSource;
console.log(downloadLink)
//<a href="https://testkimagek.s3.us-east-2.amazonaws.com/upload/image/d7f1eb91-6a65-4fde-8151-d9fb6afb7cfb_Desert.jpg" download="Desert.jpg"></a>
fileName = this.splitunderscore(linkSource)
console.log(fileName)
//Desert.jpg
if (fileName)
downloadLink.setAttribute('download', fileName);
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
splitunderscore(data){
return data.split('_')[1];
}
removeImage(){
this.removebtn = false;
// console.log(this.removebtn);
this.employeeDetails.certificateurl = "";
this.employeeDetails.showcertificateurl = "";
}
【问题讨论】:
-
可以在每个
console.log后面加注释行,看看结果输出吗? -
@Random,我已经添加了 console.log 行,你现在可以查看了。
标签: javascript angular file download