vue如何根據(jù)url下載非同源文件
一般情況下,我們根據(jù)url下載文件時可以有以下兩種方案:
1. window.open(url)2. <a :href='http://www.cgvv.com.cn/bcjs/url' rel='external nofollow' download='文件名稱'/>
但是我們在開發(fā)過程中,有時會遇到后端返回的文件地址和我們的網(wǎng)站不是同源的情況下,使用以上兩種方案下載圖片、pdf等文件,瀏覽器會直接打開,而不是下載。下面這種方法就可以解決此類情況:下載文件函數(shù)封裝:(views/util/index.js)
import axios from 'axios';// 下載文件(可解決跨域下載問題)export async function downloadFile(fileUrl, fileName) { if (!fileUrl) return; let res = await axios({ method: 'get', url: fileUrl, responseType: 'blob' }); let newUrl = window.URL.createObjectURL(res.data); let a = document.createElement('a'); a.href = newUrl; a.download = fileName; a.click(); a.remove(); //在資源下載完成后 可以人工清除createObjectURL 占用的緩存資源 window.URL.revokeObjectURL(newUrl);}
使用
<template> <div> {{file.name}} <span @click='downloadFile(file.url, file.name)'>下載</span > </div></template><script>import { downloadFile } from '@/util/index.js';export default { components: {}, data() { return { file:{ url:’https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb.zol-img.com.cn%2Fdesk%2Fbizhi%2Fimage%2F6%2F960x600%2F1426818724752.jpg&refer=http%3A%2F%2Fb.zol-img.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1627377638&t=662034e86ff9110854ca0316485e294b’, name:’美景圖’ } }; }, created() {}, mounted() {}, methods: { downloadFile }};</script><style lang='scss' scoped></style>PS:vue內(nèi)點(diǎn)擊url下載文件最佳解決方案
開發(fā)中經(jīng)常遇到這樣的功能,用戶將文件或附件上傳到服務(wù)器,后端將文件放到ftp或是其他位置,在前端頁面內(nèi)有下載的入口,有時候,后端返回的是blob,這種情況當(dāng)然是最好的,但是為了方便,后端也可能返回文件所在位置的url,這時,對前端來說,可能遇到一些問題,比如,下載文件時候?yàn)g覽器可能會出現(xiàn)閃動,下載圖片,json文件等瀏覽器支持的文件時候,不會下載,而是直接在瀏覽器內(nèi)打開這類文件,下面的方法可以完美解決此類問題
解決方案:
封裝自定義指令 將url轉(zhuǎn)成bold,在創(chuàng)建a標(biāo)簽下載blob代碼實(shí)現(xiàn)
在src 下面的 directive 文件夾下新建目錄 down-load-url down-load-url / index.js文件/* * 后端返回文件的url,前端創(chuàng)建a標(biāo)簽來下載 * * 1. 解決了若文件為圖片或?yàn)g覽器支持的格式類型,點(diǎn)擊下載會直接打開文件的問題, * 2. 下載文件時,瀏覽器會有閃動的問題 * * 頁面內(nèi)使用 * 1. 引入指令 import downLoad from ’@/directive/down-load-url’ * 2. 注冊指令 directives:{downLoad} * 3. 使用,在要下載按鈕上以指令的形式使用 例如: <el-button v-downLoad='url'>下載</el-button> */import downLoad from ’./downLoad’const install = function(Vue) { Vue.directive(’downLoadUrl’, downLoad)}downLoad.install = installexport default downLoaddown-load-url / downLoad.js文件export default { bind(el, binding) { el.addEventListener(’click’, () => { console.log(binding.value) // url const a = document.createElement(’a’) // let url = baseUrl + binding.value // 若是不完整的url則需要拼接baseURL const url = binding.value // 完整的url則直接使用 // 這里是將url轉(zhuǎn)成blob地址, fetch(url).then(res => res.blob()).then(blob => { // 將鏈接地址字符內(nèi)容轉(zhuǎn)變成blob地址a.href = URL.createObjectURL(blob)console.log(a.href)a.download = ’’ // 下載文件的名字// a.download = url.split(’/’)[url.split(’/’).length -1] // // 下載文件的名字document.body.appendChild(a)a.click() }) }) }}
到此這篇關(guān)于vue如何根據(jù)url下載非同源文件的文章就介紹到這了,更多相關(guān)vue url下載文件內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. IntelliJ IDEA刪除類的方法步驟2. Python中關(guān)于logging模塊的學(xué)習(xí)筆記3. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼4. js select支持手動輸入功能實(shí)現(xiàn)代碼5. vue使用moment如何將時間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時間格式6. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程7. Spring的異常重試框架Spring Retry簡單配置操作8. JSP中Servlet的Request與Response的用法與區(qū)別9. Struts2獲取參數(shù)的三種方法總結(jié)10. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析
