pdf 无须浏览直接下载

最近页面有下载pdf需求,之前a标签有自带的图片下载,但是pdf浏览器默认会自动浏览打开,介绍一下此方法浏览器无须打开其他页面进行下载文件

浏览器 pdf 无须浏览直接下载

支持多种文件下载

downloadFile方法下载pdf 图片等

import { download,downloadFile} from "./download.js";

downloadFile(下载文件路径,保存文件名)

/*
 *downloadPdf 下载工具
 * url 下载路径
 * name 保存的 名字
 * 
 * */
//download.js v3.0, by dandavis; 2008-2014. [CCBY2] see官网 地址http://danml.com/download.html
// data can be a string, Blob, File, or dataURL
function downloadFile(url, name) {
	var x = new XMLHttpRequest();
	x.open("GET", url, true);
	x.responseType = 'blob';
	x.onload = function(e) {
		download(x.response, name || 'shanyanwt.pdf', "image/png");
	};
	x.send();

}

function download(data, strFileName, strMimeType) {

	var self = window, // this script is only for browsers anyway...
		u = "application/octet-stream", // this default mime also triggers iframe downloads
		m = strMimeType || u,
		x = data,
		D = document,
		a = D.createElement("a"),
		z = function(a) {
			return String(a);
		},

		B = self.Blob || self.MozBlob || self.WebKitBlob || z,
		BB = self.MSBlobBuilder || self.WebKitBlobBuilder || self.BlobBuilder,
		fn = strFileName || "download",
		blob,
		b,
		ua,
		fr;

	//if(typeof B.bind === 'function' ){ B=B.bind(self); }

	if(String(this) === "true") { //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
		x = [x, m];
		m = x[0];
		x = x[1];
	}

	//go ahead and download dataURLs right away
	if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)) {
		return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs:
			navigator.msSaveBlob(d2b(x), fn) :
			saver(x); // everyone else can save dataURLs un-processed
	} //end if dataURL passed?

	try {

		blob = x instanceof B ?
			x :
			new B([x], {
				type: m
			});
	} catch(y) {
		if(BB) {
			b = new BB();
			b.append([x]);
			blob = b.getBlob(m); // the blob
		}

	}

	function d2b(u) {
		var p = u.split(/[:;,]/),
			t = p[1],
			dec = p[2] == "base64" ? atob : decodeURIComponent,
			bin = dec(p.pop()),
			mx = bin.length,
			i = 0,
			uia = new Uint8Array(mx);

		for(i; i < mx; ++i) uia[i] = bin.charCodeAt(i);

		return new B([uia], {
			type: t
		});
	}

	function saver(url, winMode) {

		if('download' in a) { //html5 A[download] 			
			a.href = url;
			a.setAttribute("download", fn);
			a.innerHTML = "downloading...";
			D.body.appendChild(a);
			setTimeout(function() {
				a.click();
				D.body.removeChild(a);
				if(winMode === true) {
					setTimeout(function() {
						self.URL.revokeObjectURL(a.href);
					}, 250);
				}
			}, 66);
			return true;
		}

		//do iframe dataURL download (old ch+FF):
		var f = D.createElement("iframe");
		D.body.appendChild(f);
		if(!winMode) { // force a mime that will download:
			url = "data:" + url.replace(/^data:([\w\/\-\+]+)/, u);
		}

		f.src = url;
		setTimeout(function() {
			D.body.removeChild(f);
		}, 333);

	} //end saver 

	if(navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
		return navigator.msSaveBlob(blob, fn);
	}

	if(self.URL) { // simple fast and modern way using Blob and URL:
		saver(self.URL.createObjectURL(blob), true);
	} else {
		// handle non-Blob()+non-URL browsers:
		if(typeof blob === "string" || blob.constructor === z) {
			try {
				return saver("data:" + m + ";base64," + self.btoa(blob));
			} catch(y) {
				return saver("data:" + m + "," + encodeURIComponent(blob));
			}
		}

		// Blob but not URL:
		fr = new FileReader();
		fr.onload = function(e) {
			saver(this.result);
		};
		fr.readAsDataURL(blob);
	}
	return true;
} /* end download() */
export default {
	download,
	downloadFile
}
export {
	download,
	downloadFile
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值