赞
踩
多方查找终于搞懂了如何去上传文件到本地服务器
<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.imagesList res.tempFiles.forEach(item => { imagesList.push(item) }) _this.setData({ imagesList: imagesList }) } }) },
上传图片
// 便利文件数组 this.data.imagesList.forEach(item =>{ wx.uploadFile({ url: _fileurl, //开发者服务器的 url filePath: item.tempFilePath, // 要上传文件资源的路径 String类型!!! name: 'image', // 文件对应的 key ,(后台用于接收的 key 参看下文 后端 接口的 方法入参 ) header: { 'content-type': 'multipart/form-data' }, // 设置请求的 header formData: {wjName:"wjName"}, // HTTP 请求中其他额外的参数 success: function (res) { debugger console.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="文件相关接口") @PassToken public 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); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。