springboot+uni-app实现图片上传

springboot的controller层

 @PostMapping("/addpic")
    public Object updatasingerPic(@RequestParam("file")MultipartFile multipartFile,@RequestParam("id")int id){
        JSONObject jsonObject=new JSONObject();
        if(multipartFile.isEmpty()){
            jsonObject.put("code",500);
            jsonObject.put("msg","文件上传失败");
            return jsonObject;
        }
        //获取从前端传过来的图片名
        String fileName = System.currentTimeMillis()+multipartFile.getOriginalFilename();
        //获取存放图片的路径
        String filePath="C://Maven//springboot//music//img//singerpic";
        File file1=new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
//        实际文件地址
        File dest=new File(filePath+"//"+fileName);
//        存储到数据库的相对文件地址
        String addmysql="/img/singerpic/"+fileName;
        try {
            multipartFile.transferTo(dest);
            QueryWrapper<Singer> wrapper=new QueryWrapper<>();
            Singer singer=new Singer();
            singer.setId(id);
            singer.setPic(addmysql);
            boolean update = singerService.updateById(singer);
            if(update){
                jsonObject.put("code",200);
                jsonObject.put("msg","文件上传成功");
                return jsonObject;
            }
            jsonObject.put("code",500);
            jsonObject.put("msg","文件上传失败");
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put("code",500);
            jsonObject.put("msg","文件上传失败+"+e.getMessage());
            e.printStackTrace();
            return jsonObject;
        }finally {
            return jsonObject;
        }
    }

uni-app

		singeravator(id){
				uni.chooseImage({
				    success: (chooseImageRes) => {
						console.log(chooseImageRes)
				        const tempFilePaths = chooseImageRes.tempFilePaths;
				        uni.uploadFile({
				            url: 'http://localhost:8080/singer/addpic', //仅为示例,非真实的接口地址
				            filePath: tempFilePaths[0],
				            name: 'file',
							formData:{
								"id":id    //此处可以把id传给后端
							},
				            success: (uploadFileRes) => {
								console.log(uploadFileRes)
				                console.log(uploadFileRes.data);
								this.getall()
				            }
				        });
				    }
				});

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以使用uni-app的canvas组件来实现电子签名。以下是实现步骤: 1. 在uni-app项目中安装canvas组件:`npm install --save uni-canvas` 2. 在需要使用电子签名的页面中引入canvas组件: ```vue <template> <view> <canvas :canvas-id="canvasId" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"></canvas> <button @click="clearCanvas">清除</button> <button @click="saveCanvas">保存</button> </view> </template> <script> import uniCanvas from 'uni-canvas'; export default { components: { uniCanvas }, data() { return { canvasId: 'myCanvas', ctx: null, isDrawing: false, lastX: 0, lastY: 0 } }, mounted() { this.ctx = uni.createCanvasContext(this.canvasId, this); }, methods: { handleTouchStart(e) { this.isDrawing = true; this.lastX = e.touches[0].x; this.lastY = e.touches[0].y; }, handleTouchMove(e) { if (!this.isDrawing) return; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(e.touches[0].x, e.touches[0].y); this.ctx.stroke(); this.lastX = e.touches[0].x; this.lastY = e.touches[0].y; }, handleTouchEnd() { this.isDrawing = false; }, clearCanvas() { this.ctx.clearRect(0, 0, uni.upx2px(375), uni.upx2px(500)); }, saveCanvas() { uni.canvasToTempFilePath({ canvasId: this.canvasId, success: function(res) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function() { uni.showToast({ title: '保存成功' }); } }); } }, this); } } } ``` 在模板中,我们使用canvas组件来绘制电子签名,同时添加了两个按钮,一个用于清除画布,一个用于保存签名。在脚本中,我们使用uni.createCanvasContext方法获取canvas上下文,然后监听touch事件来绘制签名。最后,我们使用uni.canvasToTempFilePath方法将canvas转换为图片,并使用uni.saveImageToPhotosAlbum方法将图片保存到相册中。 3. 在tsconfig.json文件中添加以下配置: ```json { "compilerOptions": { "target": "es6", "module": "esnext", "moduleResolution": "node", "strict": true, "jsx": "preserve", "sourceMap": true, "resolveJsonModule": true, "esModuleInterop": true, "lib": [ "esnext", "dom", "dom.iterable", "scripthost" ] }, "include": [ "src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx" ], "exclude": [ "node_modules" ] } ``` 在tsconfig.json文件中,我们将target设置为es6,module设置为esnext,同时开启了strict模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值