一、 form-data格式上传代码示例如下:
<el-upload
class="upload-img"
action=""
:on-remove="handleRemove"
:before-upload="beforeUpload"
list-type="picture"
:file-list="fileList"
>
<el-button size="small" type="danger">点击上传</el-button>
</el-upload>
beforeUpload(file,id) {
let fd = new FormData()
fd.append('file', file)
const obj = {
name:'',
file:''
}
dataSet.fileList = []
axios.post('/api/common/upload', fd, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(res=>{
console.log(res);
})
return false;
},
二、从File对象中读取图片数据 (base64格式) 示例
getFile(file, fileList){
console.log(file)
methods.getBase64(file.raw).then(res => {
}).then(()=>{
console.log(dataSet.imgObj);
axios.post('/api/common/upload', file, {headers: {'Content-Type': 'multipart/form-data'}}).then(res=>{
console.log(res);
})
})
},
getBase64(file) {
return new Promise(function(resolve, reject) {
let reader = new FileReader();
let imgResult = "";
reader.readAsDataURL(file);
reader.onload = function() {
imgResult = reader.result;
};
reader.onerror = function(error) {
reject(error);
};
reader.onloadend = function() {
resolve(imgResult);
};
});
},
参考文章