• fetch 上传图片 失败 bug解析


    最近在使用Taro开发上传头像功能时,发现使用 Taro.request 上传 formData 数据失败,进行原因发现,Taro.request 底层是通过封装 fetch实现的。

    fetch 作为浏览器自身提供的api,当传入的参数为 formDate 格式时,不可手动设置content-type

    方案一:删除 header 中的 content-type

    if (isFormData) {
      /**
       * fetch bug:当输入为formData格式时,不可手动设置content-type
       */
      delete header["content-type"];
    }

    方案二:通过创建 xhr 对象实现

    export const upLoadImage = (formData): Promise<any> =>
      new Promise(function(resolve, reject) {
        const request = new XMLHttpRequest();
        request.onreadystatechange = () => {
          if (request.readyState === 4 && request.status === 200) {
            const result = JSON.parse(request.responseText);
            resolve(result);
          }
        };
        request.open("POST", API_MINE_SAVE_IMAGE);
        request.setRequestHeader(
          "Authorization",
          `Bearer ${Taro.getStorageSync("token")}`
        );
        request.onerror = err => {
          reject(err);
        }; //发送请求
        request.send(formData);
      });

    .

  • 相关阅读:
    浅析MySQL二进制日志
    MySQL升级
    浅析MySQL复制
    MySQL关于exists的一个bug
    TokuDB存储引擎
    MySQL中RESET SLAVE和RESET MASTER的区别
    MySQL半同步复制
    MySQL线程池
    分析MariaDB初始化脚本mysql_install_db
    Python装饰器
  • 原文地址:https://www.cnblogs.com/crazycode2/p/14677385.html
Copyright © 2020-2023  润新知