微信小程序上传图片回调参数

像单单发送个请求wx.request直接像下面就可以了

wx.request({
      url: \'https://*************/shjz/people!card.do\',
      data: {
      },
      method: \'POST\',
      header: {
        \'content-type\': \'application/x-www-form-urlencoded;charset=utf-8\'
      },
      success: function (res) {
        console.log(res.data);
        console.log(res.data.obj[1].ckey);
        that.setData({
          dataList: res.data.obj
        })
      },
      fail: function (res) {
        console.log("--------fail--------");
      }

    })

但上传文件接口wx.uploadFile呢,我想调用返回的参数。

wx.uploadFile返回的json参数都是json所以必须先把所有参数转为json格式数据

 wx.uploadFile({
                url: \'https://**********/shjz/fileUpload!upload.do\', //仅为示例,非真实的接口地址
                filePath: tempFilePaths[0],
                name: \'file\',
                formData: {
                  \'user\': \'test\'
                },
                success(res) {
                  var data = JSON.parse(res.data);
                  // const data = res.data
                  console.log(data.success);
                  console.log(data.obj);
                  that.setData({
                    card3: data.obj
                  })
                  //do something
                }
              })

把上传文件接口返回的参数先

var data = JSON.parse(res.data);
就转为json格式的数据了剩下的就跟原来一样可以直接调用json参数了额
后台上传代码


//代表上传文件的file对象
    private File file;
    //上传文件名
    private String fileFileName;
    //旧文件名
    private String oldFileName;
    //上传文件的MIME类型
    private String fileContentType;
    //保存上传文件的目录
    private String uploadDir;

    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
    public String getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
    public String getUploadDir() {
        return uploadDir;
    }
    public void setUploadDir(String uploadDir) {
        this.uploadDir = uploadDir;
    }
    public String uploadFile() {
        return "upload";
    }
    public String uploadMod() {
        return "uploadMod";
    }
    public String upfileRar() {
        return "upfileRar";
    }

    public String upImgFile() {
        return "upImg";
    }
    public String upVideoFile() {
        return "upVideo";
    }
    public String getOldFileName() {
        return oldFileName;
    }
    public void setOldFileName(String oldFileName) {
        this.oldFileName = oldFileName;
    }
    /**
     * @方法名称: copyImg
     * @描述: 图片上传处理
     * 创建人:
     * @return void
     * @param @param srcFile
     * @param @param destFile
     * @param @throws IOException
     */
     public void upload(){
         // 得到当前时间自1970年1月1日0时0分0秒开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名。
         long now = new Date().getTime();
         //新文件名称
         String newFileName;
         Json j = new Json();
         BufferedOutputStream bos = null;
         BufferedInputStream bis = null;
         // 得到保存上传文件的目录的真实路径
         if(uploadDir==null||"".equals(uploadDir)){
             uploadDir = Constants.IMGPATH;
         }

         String path = ServletActionContext.getServletContext().getRealPath(uploadDir);

         File dir = new File(path);
         try {
             // 如果这个目录不存在,则创建它。
             if (!dir.exists())
                 dir.mkdir();
             int index = fileFileName.lastIndexOf(\'.\');
             // 判断上传文件名是否有扩展名
             if (index != -1)
                 newFileName = now + fileFileName.substring(index);
             else
                 newFileName = Long.toString(now);


             // 读取保存在临时目录下的上传文件,写入到新的文件中。

             FileInputStream fis = new FileInputStream(file);
             bis = new BufferedInputStream(fis);
             FileOutputStream fos = new FileOutputStream(new File(dir,newFileName));
             bos = new BufferedOutputStream(fos);
             byte[] buf = new byte[4096];
             int len = -1;
             while ((len = bis.read(buf)) != -1) {
                 bos.write(buf, 0, len);
             }

            // doNotNeedAuth_delImage();
             j.setSuccess(true);
             j.setObj(newFileName);
              j.setMsg("文件上传成功!");
         }catch (Exception e) {
             j.setMsg("文件上传失败!");
             e.printStackTrace();
        }  finally {
             try {
                 if (null != bis)
                     bis.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
             try {
                 if (null != bos)
                     bos.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
             writeJson(j);
         }
     }