Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过回调函数设定key的方法 #86

Open
xaoyaoo opened this issue May 24, 2024 · 0 comments
Open

通过回调函数设定key的方法 #86

xaoyaoo opened this issue May 24, 2024 · 0 comments

Comments

@xaoyaoo
Copy link

xaoyaoo commented May 24, 2024

 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];
    // 自定义上传key(即自定义上传文件名)。通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义文件名称。如果options非空,则使用options中的key作为fileName
    if (options && options.key) {
      fileName = options.key;
    }
    var formData = {
      token: config.qiniuUploadToken,
    };
    // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
    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;
        //   // this if case is a compatibility with wechat server returned a charcode, but was fixed
        //   if(res.data.hasOwnProperty('type') && res.data.type === 'Buffer'){
        //     dataString = String.fromCharCode.apply(null, res.data.data)
        //   }
        try {
          var dataObject = JSON.parse(dataString);
          // 拼接fileURL
          if (dataObject.key && dataObject.key.length > 0) {
            dataObject.fileURL = `${config.qiniuBucketURLPrefix}/${dataObject.key}`;
          } else {
            var fileURL = `${config.qiniuBucketURLPrefix}/${formData.key}`;
          }
          dataObject.fileURL = fileURL;
          // imageURL字段和fileURL字段重复,但本sdk不做删除,因为在最初版本使用的是imageURL。直接删除可能导致原有用户升级至新版sdk后出现异常。
          dataObject.imageURL = fileURL;
          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);
        }
      },
    });

经过这样修改,可以处理在formData中的key
核心修改的位置如下

          if (dataObject.key && dataObject.key.length > 0) {
            dataObject.fileURL = `${config.qiniuBucketURLPrefix}/${dataObject.key}`;
          } else {
            var fileURL = `${config.qiniuBucketURLPrefix}/${formData.key}`;
          }
@xaoyaoo xaoyaoo closed this as completed May 24, 2024
@xaoyaoo xaoyaoo reopened this May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant