前端 用a标签下载图片,设置名称无效,下载下来为file, 需要重新命名后才能打开
const downloadQRCode = (e) => {
var qrCodeUrl = apiPrefix + e.qrCode
var name2 = `${e.name}.png`
console.log(apiPrefix);
console.log(qrCodeUrl);
const link = document.createElement('a');
link.href = qrCodeUrl;
// link.download = `${e.name}.png`;
link.setAttribute('download', name2);
console.log(`${e.name}.png`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
console.log(e);
}
后来改成 Blob 设置 type: 'image/png'
const downloadQRCode = (e) => {
var qrCodeUrl = uuu + e.qrCode;
var name2 = `${e.name}.jpg`;
console.log(qrCodeUrl);
let url = window.URL.createObjectURL(new Blob([qrCodeUrl], { type: 'image/jpg' }))
let link = document.createElement('a')
link.href = url
link.style.display = 'none'
link.download = name2
document.body.appendChild(link)
link.click()
document.body.removeChild(link); // 下载完成移除元素
window.URL.revokeObjectURL(url);
}
命名有效,但打开文件显示文件损坏
这是后端代码