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)
	}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM