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());}}