首页IT科技vue下载方法(vue关于下载文件常用的几种方式)

vue下载方法(vue关于下载文件常用的几种方式)

时间2025-06-17 23:08:32分类IT科技浏览4512
导读:vue下载文件常用方式 直接打开...

vue下载文件常用方式

直接打开

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

优点:简单操作 缺点:没办法携带token

我们可以自己封装一个方法

比如如下:

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})     //创建下载的链接     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)     //点击下载     downloadElement.click()     //下载完成移除元素     document.body.removeChild(downloadElement)     if (revokeObjectURL == null || revokeObjectURL) {         //释放掉blob对象         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 }

然后在我们调用的那个页面中直接引入使用就好啦

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

这样看是不是就挺容易的

vue常用的命令

创建vue项目常用命令

如果vue项目出错了你可以把依赖删掉            ,清理一下缓存在重新安装

清理缓存 npm cache clean --force

第一步                  ,创建淘宝镜像      ,安装npm镜像(如果已经安装的就直接第二步就可以了)

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

第二步, 全局安装vue-vli

npm install --g vue-cli

第三步, 创建一个vue项目

vue init webpack 项目名

vue项目部署

npm install

启动项目

npm run dev/npm run serve

1            、安装element_ui

npm i element-ui -S

安装成功后在main.js中到导入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创建有)我们直接创建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中应用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)

总结

1.安装vue-cli

npm install --global vue-cli

2.创建一个基于 webpack 模板的新项目

vue init webpack 项目名称

3.运行:npm run dev

4.安装依赖:cd 项目名称 npm install

5.安装vue-resource插件(通过XMLHttpRequest或JSONP发起请求并处理响应 //get post请求):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)正在运行的命令         ,则ctrl +c

11.安装指定版本jquery

npm i jquery@版本号

12.若项目中node_modules文件被删除      ,使用 npm install 即能把package.json中的dependencies中所有依赖项都下载回来

以上为个人经验                  ,希望能给大家一个参考            ,也希望大家多多支持本站      。

声明:本站所有文章   ,如无特殊说明或标注                  ,均为本站原创发布         。任何个人或组织               ,在未征得本站同意时,禁止复制         、盗用                  、采集         、发布本站内容到任何网站      、书籍等各类媒体平台                  。如若本站内容侵犯了原著者的合法权益               ,可联系我们进行处理         。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
智能写作工具免费(AI智能写作助手:让优秀的软文轻而易举) wordpress发布模块(使用WordPress发布优秀软文的技巧)