uview2中的上传文件需要配合着自己后端的上传的代码
uview2 代码:
uploadFilePromise(url) {
return new Promise((resolve, reject) => {
// 后续自己封装
let a = uni.uploadFile({
//自己的请求路径
url: that.$baseURL + '/uploadAvater',
//上传的文件
filePath: that.userInfo.avatarUrl,
name: 'file',
//自己的请求头内容
header: {
Authorization: uni.getStorageSync("token")
},
success: (res) => {
setTimeout(() => {
resolve(res.data.data)
let res = JSON.parse(uploadFileRes.data);
that.userInfo.avatarUrl = res.data;
}, 1000)
}
});
})
},
java 端(若依前后端分离)接口代码:
public AjaxResult uploadAvater(MultipartFile file){
HttpServletRequest request = ServletUtils.getRequest();
StringBuffer url = request.getRequestURL();
String contextPath = request.getServletContext().getContextPath();
String urlInfo = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
try
{
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String urlFinal = urlInfo + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("url", urlFinal);
ajax.put("fileName", fileName);
ajax.put("newFileName", FileUtils.getName(fileName));
ajax.put("originalFilename", file.getOriginalFilename());
return AjaxResult.success(ajax.get("fileName"));
}
catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}