我尝试在链接上设置文件下载, 但是我无法设置所需的文档标题,默认名称为 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";
答案 0 :(得分:2)
使用属性下载定义新文件名时,它必须具有扩展名文件。 但是,您可以设置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";
答案 1 :(得分:1)
fileType = ('data:application/vnd.ms-'+type);
//type can be pdf,csv,xls......
extension ='xls';
fileName = ( name+"." + extension );
blob = new Blob( [ file ], { type: fileType } );
//file is your content
url = window.URL.createObjectURL( blob );
a = document.createElement( "a" );
// Set link on DOM.
document.body.appendChild( a );
// Set link's visibility.
a.style = "display: none";
// Set href on link.
a.href = url;
// Set file name on link.
a.download = fileName;
// Trigger click of link.
a.click();