我试图在链接上设置文件下载,
但我无法设置所需的文档标题,它的默认名称为document.pdf
这是js fiddle

的HTML

<a id='dwnldLnk'  onclick="downloadPDF();">fdfdfd </a>


JAVASCRIPT

window.downloadPDF = function downloadPDF() {
    var dlnk = document.getElementById('dwnldLnk');
    dlnk.href = pdf;
    dlnk.download = "abc.pdf";
    dlink.click();
}
var pdf = "http://fortress-api-dev.herokuapp.com/api/documents/4015fbba21c84c0d83409565662887fe";

最佳答案

当您使用属性下载定义新文件名时,它必须具有扩展名文件。
但是,您可以设置media_type,在这种情况下,请使用application / pdf:

window.downloadPDF = function downloadPDF() {
var dlnk = document.getElementById('dwnldLnk');
var media_type_pdf = "data:application/pdf;"
dlnk.href = `${media_type_pdf}${pdf}`;
dlnk.download = "abc.pdf";
//dlink.click();
}
var pdf = "http://fortress-api-dev.herokuapp.com/api/documents/4015fbba21c84c0d83409565662887fe";

10-08 11:03