将base64转成file传入后台
//将base64转换为文件
dataURLtoFile: function(dataurl, filename) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
},
upload: function() {
var self = this;
var imageName = "photo."+"jpg";
var formData = new FormData();
formData.append('file',self.dataURLtoFile(fileImg,imageName)); //fileImg base64编码
vueUI.ajaxCall({
url: "*******"+'/*****/upload',
async: false,
processData: false, //***********
contentType: false,//************
data: formData,
success: function() {
vueUI.toolTips("success", "上传成功");
window.location.reload();
},
error:function(){
vueUI.toolTips("success", "上传失败");
}
});
}
后台方法
@PostMapping("/upload")
public Response upload(@RequestParam("file") MultipartFile file) {
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
Map<String, String> resultMap = new HashMap<>(4);
resultMap.put("bucketName", CommonConstants.BUCKET_NAME); //CommonConstants.BUCKET_NAME minio上文件名
resultMap.put("fileName", fileName);
try {
minioTemplate.putObject(CommonConstants.BUCKET_NAME, fileName, file.getInputStream());
} catch (Exception e) {
log.error("上传失败", e);
return new Response().failure(CommonConstants.FAIL, e.getLocalizedMessage());
}
return new Response().success(resultMap);
}