标签的download: 是HTML5标准新增的属性,作用是指示浏览器下载URL而不是导航到URL,因此将提示用户将其保存为本地文件。

这种是定义的接口不是下载文件的路径,而是通过API可以获得文件的内容,由前端把内容写入到文件中,这种方法是通过获取文件信息,在网页上利用click事件,创建一个文件,然后将文件信息写入到文件中,然后保存

downloadFile(content, filename) {
filename = filename || 'license';
const blob = new Blob([content]);
if (window.navigator.msSaveOrOpenBlob) { // 兼容IE10
navigator.msSaveBlob(blob, filename);
} else { // 非IE
let aTag = document.createElement('a');
aTag.download = filename;
aTag.href = URL.createObjectURL(blob);
aTag.click();
URL.revokeObjectURL(aTag.href);
}
}

axios处理:我们发送请求的时候一定要写上responseType,{responseType:'arraybuffer'} 否则下载下来的文件打不开

axios.post('/fileSys/download', {
fileId: 'xxx'
},{
responseType:'arraybuffer'
}).then({
downloadFile(res.data.file, res.data.fileName);
}).catch(function (error) {
console.log(error)
})

文件上传:利用ES6的fromData来实现

function uploadFile (file) {
let formdata = new FormData()
formdata.append('file', file)
formdata.append('submit', false)
let config = {
headers: {
'Content-Type': 'multipart/form-data'
}
}
axios.post('/sysFile/upload', formdata, config).then(this.sendFileSucc)
}

 附:正常文件流下载(借助 xmlHttpRequest2)

/**
* @url {String}: 下载请求路径
* @filename {String}: 文件名
**/
function downByUrl(url,filename) {
filename = filename || 'omc.lic';
var req = new XMLHttpRequest();
req.open("GET", url, true);
//监听进度事件
req.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
console.log(percentComplete);
}
}, false); req.responseType = "blob";
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
if (typeof window.chrome !== 'undefined') {
// Chrome version
var link = document.createElement('a');
link.href = window.URL.createObjectURL(req.response);
link.download = filename;
link.click();
} else if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE version
var blob = new Blob([req.response], { type: 'application/force-download' });
window.navigator.msSaveBlob(blob, filename);
} else {
// Firefox version
var file = new File([req.response], filename, { type: 'application/force-download' });
window.open(URL.createObjectURL(file));
}
}
};
req.send();
}

vue版 文件下载的更多相关文章

  1. React版/Vue版都齐了,开源一套【特别】的后台管理系统...

    本项目主要基于Elux+Antd构建,包含React版本和Vue版本,旨在提供给大家一个简单基础.开箱即用的后台管理系统通用模版,主要包含运行环境.脚手架.代码风格.基本Layout.状态管理.路由管 ...

  2. vue版 弹幕

    效果: 下载 优化版下载: https://pan.baidu.com/s/1mvKGwJsBjXd2hvvi5Rp9pA 用法: import barrage from '../components ...

  3. vue进行文件下载

    本文为博主原创,未经允许不得转载: 总结一下,最近在vue中实现一个文件下载的功能,用了vue中ajax的方式请求下载接口, 但是返回报错,在网上查询之后,找到用ajax请求下载文件报错的原因:aja ...

  4. vue+springboot文件下载

    //vue element-ui <el-button size="medium" type="primary" @click="downloa ...

  5. 对Web(Springboot + Vue)实现文件下载功能的改进

    此为 软件开发与创新 课程的作业 对已有项目(非本人)阅读分析 找出软件尚存缺陷 改进其软件做二次开发 整理成一份博客 原项目简介 本篇博客所分析的项目来自于 ジ绯色月下ぎ--vue+axios+sp ...

  6. vue仿微信网页版|vue+web端聊天室|仿微信客户端vue版

    一.项目介绍 基于Vue2.5.6+Vuex+vue-cli+vue-router+vue-gemini-scrollbar+swiper+elementUI等技术混合架构开发的仿微信web端聊天室— ...

  7. 百度分享vue版-vshare

    vshare 基于百度分享开发的支持VUE2.X的分享插件,为您带来更多的流量!提供多种风格按钮,代码加载更快,引入社会化流量,提升网页抓取速度等优点.github地址:https://github. ...

  8. vue版 文字滚动

    用法:<marque :text="bannerText"></marque> 下载

  9. vue导出文件下载

    项目当中有用到文件的导出功能,以此来总结 request({ /*url: this.exportUrl,*/ url: `************`, method: "GET" ...

随机推荐

  1. sql server 报错处理

    1.错误:针对程序集 'StoreProc' 的 CREATE ASSEMBLY 失败,因为程序集 'StoreProc' 未获授权,不满足 PERMISSION_SET = UNSAFE.满足以下两 ...

  2. java线程池的实现机制

    原文链接:http://www.studyshare.cn/blog-front/blog/details/1133 一.java中提供的线程在开发中可能并不会直接使用,多线程编程场景使用java的线 ...

  3. javaScript:压缩图片并上传

    html代码: <input id="file" type="file" name="filesName"> js代码: var ...

  4. ubuntu 外接显示器

    xrandr --help xrandr   # 列出显示器 sudo xrandr --output eDP-1 --off   # 关闭eDP-1显示器 sudo xrandr --output ...

  5. cookies相关概念

    1.什么是Cookie Cookie实际上是一小段的文本信息.客户端请求服务器,如果服务器需要记录该用户状态,就使用response向客户端浏览器颁发一个Cookie.客户端浏览器会把Cookie保存 ...

  6. board_key.h/board_key.c

    /******************************************************************************* Filename: board_key ...

  7. Loadrunner 50个Vusers并发创建3000个账号脚本调试以及场景运行

    目标:用50个并发用户创建3000个账号到HP Web Server 分析: 1. Vugen录制脚本时,打开首页http://127.0.0.1:1080/WebTours/,进入注册账号界面不用重 ...

  8. Chen qiaoqiao Studio

    Welcome here! If you need any help, please contact us. Contact info Email: lovey_kids@163.com

  9. Kickstart自动化安装平台

    PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并 ...

  10. 初识docker

    什么是docker Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目,于 2013 年 3 月以 Apache 2.0 授权协议开源,主 ...