vue 上传和下载文件/图片

vue 上传和下载文件/图片

  1. 前端上传图片/文件(fromData格式)
// 接口参数
export function add(formdata) {
    return request({
        url: '',
        method: 'post',
        headers: { 'Content-Type': 'multipart/form-data' },
        data: formdata
    })
}

// 传入的formdata值
var formdata = new FormData();
// 使用elementUI的上传控件,返回的file对象
formdata.append("file", this.file.row);

  1. 前端下载文件/展示图片(blob)
// 接口参数
function getFile(name) {
	return service({
		url: '/file/downloadFileRedis?fileName='+name,
		method: 'get',
		responseType:'blob'	
	})

// 文件下载
getFile(this.fileName).then(res =>{
    const blob = new Blob([res],{type:Headers['content-type']});
    if('download' in document.createElement('a')){
        //非IE下载
        const a = document.createElement('a');
        a.download = this.fileName;
        a.style.display = 'none';
        a.href = window.URL.createObjectURL(blob);
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(a.href);
        document.body.removeChild(a);
    }else{
        //IE10+下载
        if(typeof window.navigator.msSaveBlob !== 'undefined'){
        window.navigator.msSaveBlob(blob, _this.selected);
        }else{
        let URL = window.URL || window.webkitURL;
        let downloadUrl = URL.createObjectURL(blob);
        window.location = downloadUrl;
    }
    }    
})


// 图片展示
getFile(this.fileName).then(res =>{
 const blob = new Blob([res],{type:Headers['content-type']});
    let url = window.URL.createObjectURL(blob);
    this.imgurl1 = url
    return true
})


// SVG图片展示
getFile(this.fileName).then(res =>{
 const blob = new Blob([res],{type:Headers['image/svg+xml;charset=utf-8']});
    let url = window.URL.createObjectURL(blob);
    this.imgurl1 = url
    return true
})


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值