• 微信小程序上传图片到服务端,springboot项目。避免踩坑保姆教程


    多方查找终于搞懂了如何去上传文件到本地服务器

    前端代码

      
            
              
            
    
            
              
              
            
          
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    小程序端代码

    选择图片

      // 上传图片
      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
            })
          }
        })
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    上传图片

    // 便利文件数组 
     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);
          }
         })
      })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    服务端代码

    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);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    从01背包说起(中)
    【MATLAB】史上最全的11种数字信号滤波去噪算法全家桶
    Real- Time Rendering-图形渲染管线(The graphics rendering pipeline)
    [附源码]java毕业设计汽车租赁管理系统-
    git log 统计自己代码提交量报错 awk No such file or directory
    【Java】如何将二进制转换成MultipartFile
    【Matlab】状态空间模型的最小化实现 minreal() 函数
    【JVM】JVM表示浮点数
    路由器bgp协议对接生产K8S集群网络(Calico)
    1054 The Dominant Color
  • 原文地址:https://blog.csdn.net/qq_43406318/article/details/136682264