收藏
回答

关于wx.uploadFile

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.uploadFile 工具 6.6.7 1.9.98

在使用wx.uploadFileAPI的时候,成功之后返回的data为null?这个是什么,求解


正常来讲他这个data中是应该有值的啊

回答关注问题邀请回答
收藏

4 个回答

  • 修行
    修行
    2020-10-26

    我这边公众号前端页面上传的图片没有生成预览 同时没有保存到服务器 在后台也不显示,请问大家知道怎样解决吗?

    2020-10-26
    有用
    回复
  • See
    See
    2020-01-17

    你好,怎么做上传图片到七牛

    2020-01-17
    有用
    回复 1
    • 李成林
      李成林
      2020-03-25
      才看见,你现在会了吗,还不会的话我应该可以帮忙
      2020-03-25
      回复
  • 李成林
    李成林
    2018-07-05

    已经知道什么问题了

    2018-07-05
    有用
    回复 1
  • 极学者@陈序员
    极学者@陈序员
    2018-07-05

    代码贴出来 分析分析

    2018-07-05
    有用
    回复 5
    • 李成林
      李成林
      2018-07-05

      我这个是想把图片传到七牛,这个是起因。下面的是sdk

      // created by gpake
      (function() {
       
      var config = {
          qiniuRegion: '',
          qiniuImageURLPrefix: '',
          qiniuUploadToken: '',
          qiniuUploadTokenURL: '',
          qiniuUploadTokenFunction: null,
          qiniuShouldUseQiniuFileName: false
      }
       
      module.exports = {
          init: init,
          upload: upload,
      }
       
      // 在整个程序生命周期中,只需要 init 一次即可
      // 如果需要变更参数,再调用 init 即可
      function init(options) {
          config = {
              qiniuRegion: '',
              qiniuImageURLPrefix: '',
              qiniuUploadToken: '',
              qiniuUploadTokenURL: '',
              qiniuUploadTokenFunction: null,
              qiniuShouldUseQiniuFileName: false
          };
          updateConfigWithOptions(options);
      }
       
      function updateConfigWithOptions(options) {
          if (options.region) {
              config.qiniuRegion = options.region;
          } else {
              console.error('qiniu uploader need your bucket region');
          }
          if (options.uptoken) {
              config.qiniuUploadToken = options.uptoken;
          } else if (options.uptokenURL) {
              config.qiniuUploadTokenURL = options.uptokenURL;
          } else if(options.uptokenFunc) {
              config.qiniuUploadTokenFunction = options.uptokenFunc;
          }
          if (options.domain) {
              config.qiniuImageURLPrefix = options.domain;
          }
          config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
      }
       
      function upload(filePath, success, fail, options, progress, cancelTask) {
          if (null == filePath) {
              console.error('qiniu uploader need filePath to upload');
              return;
          }
          if (options) {
            updateConfigWithOptions(options);
          }
          if (config.qiniuUploadToken) {
              doUpload(filePath, success, fail, options, progress, cancelTask);
          } else if (config.qiniuUploadTokenURL) {
              getQiniuToken(function() {
                  doUpload(filePath, success, fail, options, progress, cancelTask);
              });
          } else if (config.qiniuUploadTokenFunction) {
              config.qiniuUploadToken = config.qiniuUploadTokenFunction();
              if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
                  console.error('qiniu UploadTokenFunction result is null, please check the return value');
                  return
              }
              doUpload(filePath, success, fail, options, progress, cancelTask);
          } else {
              console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
              return;
          }
      }
       
      function doUpload(filePath, success, fail, options, progress, cancelTask) {
          if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
              console.error('qiniu UploadToken is null, please check the init config or networking');
              return
          }
          var url = uploadURLFromRegionCode(config.qiniuRegion);
          var fileName = filePath.split('//')[1];
          if (options && options.key) {
              fileName = options.key;
          }
          var formData = {
              'token': config.qiniuUploadToken
          };
          if (!config.qiniuShouldUseQiniuFileName) {
            formData['key'] = fileName
          }
          var uploadTask = wx.uploadFile({
              url: url,
              filePath: filePath,
              name: 'file',
              formData: formData,
              success: function (res) {
                var dataString = res.data
                if(res.data.hasOwnProperty('type') && res.data.type === 'Buffer'){
                  dataString = String.fromCharCode.apply(null, res.data.data)
                }
                try {
                  var dataObject = JSON.parse(dataString);
                  //do something
                  var imageUrl = config.qiniuImageURLPrefix + '/' + dataObject.key;
                  dataObject.imageURL = imageUrl;
                  console.log(dataObject);
                  if (success) {
                    success(dataObject);
                  }
                } catch(e) {
                  console.log('parse JSON failed, origin String is: ' + dataString)
                  if (fail) {
                    fail(e);
                  }
                }
              },
              fail: function (error) {
                  console.error(error);
                  if (fail) {
                      fail(error);
                  }
              },
              complete:function(res){
                console.log(res)
              }
          })
       
          uploadTask.onProgressUpdate((res) => {
              progress && progress(res)
          })
       
          cancelTask && cancelTask(() => {
              uploadTask.abort()
          })
      }
       
      function getQiniuToken(callback) {
        wx.request({
          url: config.qiniuUploadTokenURL,
          success: function (res) {
            var token = res.data.uptoken;
            if (token && token.length > 0) {
              config.qiniuUploadToken = token;
              if (callback) {
                  callback();
              }
            } else {
              console.error('qiniuUploader cannot get your token, please check the uptokenURL or server')
            }
          },
          fail: function (error) {
            console.error('qiniu UploadToken is null, please check the init config or networking: ' + error);
          }
        })
      }
       
      function uploadURLFromRegionCode(code) {
          var uploadURL = null;
          switch(code) {
              case 'ECN': uploadURL = 'https://up.qbox.me'; break;
              case 'NCN': uploadURL = 'https://up-z1.qbox.me'; break;
              case 'SCN': uploadURL = 'https://up-z2.qbox.me'; break;
              case 'NA': uploadURL = 'https://up-na0.qbox.me'; break;
              case 'ASG': uploadURL = 'https://up-as0.qbox.me'; break;
              default: console.error('please make the region is with one of [ECN, SCN, NCN, NA, ASG]');
          }
          return uploadURL;
      }
       
      })();


      2018-07-05
      回复
    • 李成林
      李成林
      2018-07-05
      const qiniuUploader = require("../../utils/qiniuUploader");
      //index.js
       
      // 初始化七牛相关参数
      function initQiniu() {
        var options = {
          region: 'SCN', // 华北区
          uptokenURL: '',
          shouldUseQiniuFileName: true
        };
        qiniuUploader.init(options);
      }
       
      //获取应用实例
      var app = getApp()
      Page({
        data: {
          imageObject: {}
        },
        //事件处理函数
        onLoad: function () {
          console.log('onLoad')
          var that = this;
        },
        didPressChooesImage: function() {
          var that = this;
          didPressChooesImage(that);
        },
          didCancelTask: function() {
            this.data.cancelTask()
          }
      });
       
      function didPressChooesImage(that) {
        initQiniu();
        // 微信 API 选文件
        wx.chooseImage({
            count: 1,
            success: function (res) {
              var filePath = res.tempFilePaths[0];
              // 交给七牛上传
              qiniuUploader.upload(filePath, (res) => {
                that.setData({
                  'imageObject': res
                });
              }, (error) => {
                console.error('error: ' + JSON.stringify(error));
              },
              // , {
              //     region: 'NCN', // 华北区
              //     uptokenURL: 'https://[yourserver.com]/api/uptoken',
              //     domain: 'http://[yourBucketId].bkt.clouddn.com',
              //     shouldUseQiniuFileName: false
              //     key: 'testKeyNameLSAKDKASJDHKAS'
              //     uptokenURL: 'myServer.com/api/uptoken'
              // }
              null,// 可以使用上述参数,或者使用 null 作为参数占位符
              (progress) => {
                console.log('上传进度', progress.progress)
                  console.log('已经上传的数据长度', progress.totalBytesSent)
                  console.log('预期需要上传的数据总长度', progress.totalBytesExpectedToSend)
              }, cancelTask => that.setData({cancelTask})
              );
            }
          })
      }

      这个是测试页面的js,图片能够传到七牛,但是这个API的data没有返回来

      2018-07-05
      回复
    • 极学者@陈序员
      极学者@陈序员
      2018-07-05

      写了 有点庞大的 没看懂

      自己还是很菜的 不好意思

      2018-07-05
      回复
    • 李成林
      李成林
      2018-07-05

      我好像知道是什么问题了

      2018-07-05
      1
      回复
    • 极学者@陈序员
      极学者@陈序员
      2018-07-05回复李成林

      恭喜 ^_^

      2018-07-05
      回复
登录 后发表内容