多方查找终于搞懂了如何去上传文件到本地服务器
前端代码
<view class="operation_row common_mb0"><view class="upload_btn" bindtap="clickUpload"><image src="../../common/images/icon/icon02.png"></image></view><view class="upload_img" wx:for="{{imagesList}}" wx:key="index"><image class="upload_img_del" src="../../common/images/icon/icon03.png" data-index="{{index}}" bindtap="clickDelImg"></image><image src="{{item.tempFilePath}}"></image></view></view>
小程序端代码
选择图片
// 上传图片clickUpload() {const _this = this; wx.chooseMedia({count: 9,mediaType: ['image'],sourceType: ['album', 'camera'],camera: 'back',success(res) {let imagesList = _this.data.imagesListres.tempFiles.forEach(item => {imagesList.push(item)}) _this.setData({imagesList: imagesList})}})},
上传图片
// 便利文件数组 this.data.imagesList.forEach(item =>{wx.uploadFile({url: _fileurl, //开发者服务器的 urlfilePath: item.tempFilePath, // 要上传文件资源的路径 String类型!!!name: 'image', // 文件对应的 key ,(后台用于接收的 key 参看下文 后端 接口的 方法入参 )header: {'content-type': 'multipart/form-data'}, // 设置请求的 headerformData: {wjName:"wjName"}, // HTTP 请求中其他额外的参数success: function (res) {debuggerconsole.log(res);},fail: function (res) {console.log(res);}})})
服务端代码
uploadFileMinIo(String wjName, MultipartFile image)
wjName: 对应formData: {wjName:“wjName”}
image:对应name: ‘image’, // 文件对应的 key
/*** 上传文件MinIo*/@ApiVersion(value = ApiVersionConsts.API_V1,group = ApiVersionConsts.SWAGGER_API_V1)@RequestMapping("/wxupload")@ApiOperation(value="上传文件", notes="文件相关接口")@PassTokenpublic Result uploadFileMinIo(String wjName, MultipartFile image) throws Exception {// 上传到minio . 上传到本地 请百度吧,String bucketName = minioProperties.getBucketName();String fileName = minIoUtil.minioUpload(image, image.getOriginalFilename(), bucketName);String url = minIoUtil.getShowUtrl(fileName, bucketName);TWjxx build = TWjxx.builder().filename(fileName).url(url).build();//格式化时间戳SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");String nowTime = sdf.format(new Date().getTime());//取得图片的格式后缀String originalLastName = image.getOriginalFilename();String picLastName = originalLastName.substring(originalLastName.lastIndexOf("."));//拼接:名字+时间戳+后缀String picName = nowTime+"." + wjName + picLastName;return Result.success(build);}