亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue關(guān)于下載文件常用的幾種方式

 更新時(shí)間:2022年10月12日 11:07:54   作者:憤怒的小前端  
這篇文章主要介紹了vue關(guān)于下載文件常用的幾種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue下載文件常用方式

直接打開

直接打開是指我們直接使用window.open(URL)的方法

  • 優(yōu)點(diǎn):簡(jiǎn)單操作
  • 缺點(diǎn):沒辦法攜帶token

我們可以自己封裝一個(gè)方法

比如如下:

import axios from "axios"
import * as auth from '@/utils/auth.js'
let ajax = axios.create({
? ? baseURL: process.env.VUE_APP_BASE_API,
? ? timeout: 100000
});
ajax.interceptors.request.use(config => {
? ? ? ? config.headers = {
? ? ? ? ? ? Authorization: auth.getToken(),
? ? ? ? ? ? // OrgId: auth.getUser().orgId,
? ? ? ? ? ? // UserId: auth.getUser().id,
? ? ? ? }
? ? ? ? return config
? ? },
? ? err => {
? ? ? ? return Promise.reject(err)
? ? })
let downloadFile = async (url, formData, options) => {
? ? await ajax.post(url, formData, {responseType: 'arraybuffer'}).then(resp => download(resp, options))
}
let getFile = async (url, options) => {
? ? await ajax.get(url, {responseType: 'blob'}).then(resp => download(resp, options))
}
let download = (resp, options) => {
? ? let blob = new Blob([resp.data], {type: options.fileType ? options.fileType : 'application/octet-binary'})
? ? //創(chuàng)建下載的鏈接
? ? let href = window.URL.createObjectURL(blob)
? ? downloadBlob(href, options.fileName)
}
let downloadBlob = (blobUrl, fileName, revokeObjectURL) => {
? ? let downloadElement = document.createElement('a')
? ? downloadElement.href = blobUrl
? ? //下載后文件名
? ? downloadElement.download = fileName
? ? document.body.appendChild(downloadElement)
? ? //點(diǎn)擊下載
? ? downloadElement.click()
? ? //下載完成移除元素
? ? document.body.removeChild(downloadElement)
? ? if (revokeObjectURL == null || revokeObjectURL) {
? ? ? ? //釋放掉blob對(duì)象
? ? ? ? window.URL.revokeObjectURL(blobUrl)
? ? }
}
let getDownloadFileUrl = async (url, fileType) => {
? ? let blob
? ? await ajax.get(url, {responseType: 'blob'}).then(resp => {
? ? ? ? blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
? ? })
? ? return window.URL.createObjectURL(blob);
}
let getDownloadFileUrlByPost = async (url, data, fileType) => {
? ? let blob
? ? await ajax.post(url, data, {responseType: 'blob'}).then(resp => {
? ? ? ? blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
? ? })
? ? return window.URL.createObjectURL(blob);
}
let getDownloadFileBlob = async (url, fileType) => {
? ? let blob
? ? await ajax.get(url, {responseType: 'blob'}).then(resp => {
? ? ? ? blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
? ? })
? ? return blob;
}
export default {
? ? ajax,
? ? downloadFile,
? ? getFile,
? ? getDownloadFileUrl,
? ? getDownloadFileUrlByPost,
? ? getDownloadFileBlob,
? ? downloadBlob
}

然后在我們調(diào)用的那個(gè)頁面中直接引入使用就好啦

//先引用
import ajax from '../../utils/ajax.js'
//使用
ajax.downloadFile('URL',null,{下載的文件名稱})

這樣看是不是就挺容易的

vue常用的命令

創(chuàng)建vue項(xiàng)目常用命令

如果vue項(xiàng)目出錯(cuò)了你可以把依賴刪掉,清理一下緩存在重新安裝

清理緩存     npm cache clean --force

第一步,創(chuàng)建淘寶鏡像,安裝npm鏡像(如果已經(jīng)安裝的就直接第二步就可以了)

npm install -g cnpm --registry=https://registry.npm.taobao.org

第二步, 全局安裝vue-vli

npm install --g vue-cli

第三步, 創(chuàng)建一個(gè)vue項(xiàng)目

vue init webpack 項(xiàng)目名 

vue項(xiàng)目部署

npm install

啟動(dòng)項(xiàng)目

npm run dev/npm run serve

1、安裝element_ui

npm i element-ui -S

安裝成功后在main.js中到導(dǎo)入element-ui,并使用

? ? import ElementUI from 'element-ui';
? ? import 'element-ui/lib/theme-chalk/index.css';
? ? Vue.use(ElementUI);

2、安裝vue_router

npm install vue-router --save?

安裝完成如果沒有文件夾router(一般都有vue2.0下需要自己選擇安裝,vue3.0創(chuàng)建有)我們直接創(chuàng)建index.js文件。文件放入以下配置

import Vue from 'vue'
import VueRouter from 'vue-router';
Vue.use(VueRouter);?
import XXX "../src/components/xxx";
??
const routes = [
? {
? ? path:'/'
? ? component: XXX
? },?
]
?
const router = new VueRouter({
? routes
})
?
export default router

要在main.js中應(yīng)用router.js文件。我們需要在中放入router

import router from "../router";
?? ?
new Vue({
? router,
? el: '#app',
? components: { App },
? template: '<App/>'
})

3、 安裝axios組件,安裝命令如下:

npm install --save vue-axios ?

 在main.js文件下引入如下代碼:

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

總結(jié) 

1.安裝vue-cli

npm install --global vue-cli

2.創(chuàng)建一個(gè)基于 webpack 模板的新項(xiàng)目

vue init webpack 項(xiàng)目名稱

3.運(yùn)行:npm run dev

4.安裝依賴:cd 項(xiàng)目名稱 npm install

5.安裝vue-resource插件(通過XMLHttpRequest或JSONP發(fā)起請(qǐng)求并處理響應(yīng) //get post請(qǐng)求):npm install vue-resource --save

6.安裝路由插件:

npm install vue-router --save

7.安裝element-ui:

npm i element-ui -S(安裝好之后要記得在main.js中引入)

8.安裝axios npm install axios --save

9.安裝Echarts npm install echarts --save

10.如果想要終止終端(cmd)正在運(yùn)行的命令,則ctrl +c

11.安裝指定版本jquery

npm i jquery@版本號(hào)

12.若項(xiàng)目中node_modules文件被刪除,使用 npm install 即能把package.json中的dependencies中所有依賴項(xiàng)都下載回來

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論