小程序与springboot结合上传删除、预览图片

小程序前端

wxml:

	<view class="item" bindtap="" style="border:none;padding-bottom:0" wx:if="{{srcbasset.image}}">
		资产图片
	</view>
	<view class="item" bindtap="" style="border-bottom: 15px solid #f3f3f3;" wx:if="{{srcbasset.image}}">
		<view class="group-body">
			<view class="img-upload">
				<view class="img-item img-item-item">
					<image src="{{_server}}{{srcbasset.image}}" mode="aspectFit" bindtap='showImg' style="width:120rpx;height:120rpx;"></image>
				</view>
			</view>
		</view>
	</view>

	<view class="item" bindtap="" style="border-bottom: 15px solid #f3f3f3;">
		<view class="btn" bindtap='chooseImg'>资产图片上传+</view>
		<view class="btn" bindtap='deleteImg' wx:if="{{srcbasset.image}}" style="background:#ffc107;">删除</view>
	</view>

 

js

//展示图片
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: [that.data._server + that.data.srcbasset.image],
      current: that.data._server + that.data.srcbasset.image
    })
  },
  deleteImg: function (e) {
    let that = this;
    const params = {
      id: that.data.srcbasset.id
    }
    request.GET('miniapp/deleteImg', params, function (res) {
      if (res.data && res.data.code === 0) {
        wx.hideToast();
        var a='srcbasset.image';
        that.setData({
          [a]:''
        });
      } else {
        wx.hideToast();
      }
    }, function (res) {
      wx.hideToast();
    });
  },
  chooseImg: function (e) {
    let that = this;
    let id=this.data.srcbasset.id;
    wx.chooseImage({
      count: 9,
      sizeType: [],
      sourceType: ['album', 'camera'],
      success: function (res) {
        let tempFilePaths = res.tempFilePaths;
        for (let i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data._server + 'miniapp/uploadImage',
            filePath: tempFilePaths[i],
            name: 'file',
            header: { 
              'Content-Type': 'multipart/form-data',
              'Cookie': 'JSESSIONID=' + app.cache.access_token
            },
            formData: {
              id:id
            },
            success: function (res) {
              wx.hideLoading();
              var a='srcbasset.image';
              that.setData({
                [a]: JSON.parse(res.data).msg
              });
            }
          });
          wx.showLoading({
            title: '正在上传...',
          })
        }
      },
      fail: function (res) {
        console.log(res, '失败');
      },
      complete: function (res) { },
    });
  }

 

Springboot后台

    /**
     * 非IT上传图片
     */
    @PostMapping("/uploadImage")
    @ResponseBody
    public AjaxResult uploadImage(@RequestParam("id") Long id,MultipartFile file)throws IOException {
        if (!file.isEmpty()) {
            String image = FileUploadUtils.upload(RuoYiConfig.getSrcbItassetPath(),file);
            SrcbAssetinfo srcbAssetinfo=new SrcbAssetinfo();
            srcbAssetinfo.setId(id);
            srcbAssetinfo.setImage(image);
            srcbAssetinfoService.updateSrcbAssetinfo(srcbAssetinfo);
            System.out.println(image);
            return AjaxResult.success(image);
        }else{
            return AjaxResult.success("上传图片不存在!");
        }
    }


    /**
     * 删除非IT上传图片
     */
    @GetMapping("/deleteImg")
    @ResponseBody
    public AjaxResult deleteImg(@RequestParam("id") Long id) {
        SrcbAssetinfo srcbAssetinfo=srcbAssetinfoService.selectSrcbAssetinfoById(id);
        SrcbAssetinfo srcbAssetinfo2=new SrcbAssetinfo();
        srcbAssetinfo2.setId(srcbAssetinfo.getId());
        srcbAssetinfo2.setImage("");
        return AjaxResult.success( srcbAssetinfoService.updateSrcbAssetinfo(srcbAssetinfo2));
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值