URLToPDF 是PDF的路径地址。
theFileName 是 PDF路径上的名字,当然也可以自定义。
var URLToPDF = "";
var theFileName = URLToPDF.substring(URLToPDF.lastIndexOf('/') + 1, URLToPDF.length);
var URL = window.URL || window.webkitURL;
function saveAs(blob, filename) {
var type = blob.type;
var force_saveable_type = 'application/octet-stream';
if (type && type != force_saveable_type) {
var slice = blob.slice || blob.webkitSlice || blob.mozSlice;
blob = slice.call(blob, 0, blob.size, force_saveable_type);
}
var url = URL.createObjectURL(blob);
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = url;
save_link.download = filename;
var event = new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window
});
save_link.dispatchEvent(event);
URL.revokeObjectURL(url);
}
$('#downloads').on('click', function (){
var oReq = new XMLHttpRequest();
oReq.open("GET", URLToPDF, true);
oReq.responseType = "blob";
oReq.onload = function() {
var file = new Blob([oReq.response], {
type: 'application/pdf'
});
saveAs(file, theFileName);
};
oReq.send();
})