本文整理了前端经常使用的下载文件以及上传文件的方法
例子均以vue
+element ui
+axios
为例,不使用el
封装好的上传组件,这里自行进行封装实现前端
以图片为例,文件上传能够省略预览图片功能
图片上传可使用2种方式:文件流
和base64
;vue
上传
+预览
:<input type="file" id='imgBlob' @change='changeImgBlob' /> <el-image style="width: 100px; height: 100px" :src="imgBlobSrc"></el-image>
// data imgBlobSrc: "" // methods changeImgBlob() { let file = document.querySelector("#imgBlob"); /** *图片预览 *更适合PC端,兼容ie7+,主要功能点是window.URL.createObjectURL */ var ua = navigator.userAgent.toLowerCase(); if (/msie/.test(ua)) { this.imgBlobSrc = file.value; } else { this.imgBlobSrc = window.URL.createObjectURL(file.files[0]); } //上传后台 const fd = new FormData(); fd.append("files", file.files[0]); fd.append("xxxx", 11111); //其余字段,根据实际状况来 axios({ url: "/yoorUrl", //URL,根据实际状况来 method: "post", headers: { "Content-Type": "multipart/form-data" }, data: fd }); }
浏览器network
查看img
元素查看src
,为blob
类型ios
上传
+预览
:<input type="file" id='imgBase' @change='changeImgBase' /> <el-image style="width: 100px; height: 100px" :src="imgBaseSrc"></el-image>
// data imgBaseSrc : "" // methods changeImgBase() { let that = this; let file = document.querySelector("#imgBase"); /** *图片预览 *更适合H5页面,兼容ie10+,图片base64显示,主要功能点是FileReader和readAsDataURL */ if (window.FileReader) { var fr = new FileReader(); fr.onloadend = function (e) { that.imgBaseSrc = e.target.result; //上传后台 axios({ url: "/yoorUrl", //URL,根据实际状况来 method: "post", data: { files: that.imgBaseSrc } }); }; fr.readAsDataURL(file.files[0]); } }
浏览器network
查看axios
img
元素查看src
,为base64
类型segmentfault
假设须要下载图片为url
,文件流
处理和这个同样浏览器
<el-image style="width: 100px; height: 100px" :src="downloadImgSrc"></el-image> <el-button type="warning" round plain size="mini" @click='downloadImg'>点击下载</el-button>
responseType
为blob
//data downloadImgSrc:'https://i.picsum.photos/id/452/400/300.jpg?hmac=0-o_NOka_K6sQ_sUD84nxkExoDk3Bc0Qi7Y541CQZEs' //methods downloadImg() { axios({ url: this.downloadImgSrc, //URL,根据实际状况来 method: "get", responseType: "blob" }).then(function (response) { const link = document.createElement("a"); let blob = new Blob([response.data], { type: response.data.type }); let url = URL.createObjectURL(blob); link.href = url; link.download = `实际须要的文件名.${response.data.type.split('/')[1]}`; link.click(); document.body.removeChild(link); }); }
<el-image style="width: 100px; height: 100px" :src="downloadImgSrc"></el-image> <el-button type="warning" round plain size="mini" @click='downloadImg'>点击下载</el-button>
responseType
为blob
//data downloadImgSrc:'https://i.picsum.photos/id/452/400/300.jpg?hmac=0-o_NOka_K6sQ_sUD84nxkExoDk3Bc0Qi7Y541CQZEs' //methods downloadImg() { axios({ url: this.downloadImgSrc, //URL,根据实际状况来 method: "get", responseType: "blob" }).then(function (response) { const link = document.createElement("a"); let blob = new Blob([response.data], { type: response.data.type }); let url = URL.createObjectURL(blob); link.href = url; link.download = `实际须要的文件名.${response.data.type.split('/')[1]}`; link.click(); document.body.removeChild(link); }); }
图片上传可使用2种方式文件流
和Base64
图片下载同理。
更多问题欢迎加入前端交流群交流749539640app