vue pdf下载(非预览)

只需改掉 选择器名称 和 图片存放的URL 即可使用

downloadimg(){
let _this=this
let url = 'https://PDF或者图片路径/Merged.pdf'
		    let name = '下载的文件名.pdf'

// 发送http请求,将文件链接转换成文件流
_this.fileAjax(url, function(xhr) {
_this.downloadFile(xhr.response, name)
}, {
responseType: 'blob'
})

},

	function fileAjax(url, callback, options) {
	    let xhr = new XMLHttpRequest()
	    xhr.open('get', url, true)
	    if (options.responseType) {
	        xhr.responseType = options.responseType
	    }
	    xhr.onreadystatechange = function() {
	        if (xhr.readyState === 4 && xhr.status === 200) {
	            callback(xhr)
	        }
	    }
	    xhr.send()
	}
	
	function downloadFile(content, filename) {
	    window.URL = window.URL || window.webkitURL
	    let a = document.createElement('a')
	    let blob = new Blob([content])
	// 通过二进制文件创建url
	    let url = window.URL.createObjectURL(blob)
	    a.href = url
	    a.download = filename
	    a.click()
	// 销毁创建的url
	    window.URL.revokeObjectURL(url)
	}
posted @ 2020-10-12 14:13  看看时间  阅读(775)  评论(0编辑  收藏  举报