huaweicloud / huaweicloud-sdk-browserjs-obs

Apache License 2.0
30 stars 23 forks source link

使用putObject,web上传图片报错 SyntaxError: Unexpected end of JSON input #38

Open dm66 opened 1 year ago

dm66 commented 1 year ago
"esdk-obs-browserjs": "^3.23.5"

下面是上传图片的方法:

/**
 * 上传文件
 */
export const uploadFile = (file) => {
  const objectName = getRandomString(16) + "." + file.name?.split(".")[1];
  // 创建ObsClient实例
  const obsClient = new ObsClient(uploadObsConfig);

  return obsClient
    .putObject({
      Bucket: uploadObsConfig.bucketname, // 桶名
      SourceFile: file,
      Key: objectName, // 对象名
      RequestDate: new Date(),
    })
    .then(function (result) {
      if (result.CommonMsg.Status < 300) {
        if (result.InterfaceResult) {
          const url = `https://${uploadObsConfig.bucketname}.****/${objectName}`;
          console.log("上传成功", url);
          return url;
        }
      }
      return "";
    })
    .catch(function (err) {
      console.error("Error-->" + err);
      return "";
    });
};

选择图片后,代码走进catch方法,报错如下:

image

函数入参File:

image

请问是哪里出了错?帮忙看看

liqiuqiu111 commented 6 months ago

仅仅依据截图内容无法判断出具体的错误原因,如果有完整的错误堆栈可以提供一下; 一般这种错误的原因在于json体未传完,或者json内容的格式不正确。