将formData与axios一起使用时,source.on不是函数

bttbmeg0  于 2022-12-04  发布在  iOS
关注(0)|答案(1)|浏览(145)

在此处使用fromData时遇到问题

const res = await axios.post(
      clientPayload?.uploadLink,
      { data: formData },
      {
        headers: {
          'content-type': 'multipart/form-data',
        },
      }
    );

它与请求包一起工作,但与axios一起工作时,它显示了此错误。请提供任何帮助,这是我的函数
``

export const uploadVideo = async (title: string, file: any) => {
  const formData = new FormData();
  try {
    const response = await axios.put(
      `https://url.com/api/videos`,
      {},
      {
        params: {
          title,
        },
        headers: {
          Authorization: `Apisecret ${process.env.VIDEO_SECRET}`,
          Accept: 'application/json',
          ['Content-Type']: 'application/json',
        },
      }
    );

    const data = await response?.data;
    const clientPayload = data?.clientPayload;
    formData.append('policy', clientPayload?.policy);
    formData.append('key', clientPayload?.key);
    formData.append('x-amz-signature', clientPayload['x-amz-signature']);
    formData.append('x-amz-algorithm', clientPayload['x-amz-algorithm']);
    formData.append('x-amz-date', clientPayload['x-amz-date']);
    formData.append('x-amz-credential', clientPayload['x-amz-credential']);
    formData.append('success_action_status', '201');
    formData.append('success_action_redirect', '');
    formData.append('file', {
      filepath: fs.createReadStream(file.path),
      filename: file.path,
      contentType: null,
    });

    const res = await axios.post(
      clientPayload?.uploadLink,
      { data: formData },
      {
        headers: {
          'content-type': 'multipart/form-data',
        },
      }
    );

    return res;
  } catch (error) {
    if (axios.isAxiosError(error)) {
      logger.error('error message: ', error);
    } else {
      logger.error('unexpected error: ', error);
    }
  }
};

``

brvekthn

brvekthn1#

尝试使用URLSearchParams而不是form-data执行此操作

export const uploadVideo = async (title, file) => {
    try {
        const response = await axios.put(
            `https://url.com/api/videos`,
            {},
            {
                params: {
                    title,
                },
                headers: {
                    Authorization: `Apisecret ${process.env.VIDEO_SECRET}`,
                    Accept: 'application/json',
                    'Accept-Encoding': 'application/json',
                    ['Content-Type']: 'application/json'
                },
            }
        );

        const data = await response?.data;
        const clientPayload = data?.clientPayload;
        const res = await axios.post(
            clientPayload?.uploadLink,
            new URLSearchParams({
                'policy': clientPayload?.policy,
                'key': clientPayload?.key,
                'x-amz-signature': clientPayload['x-amz-signature'],
                'x-amz-algorithm': clientPayload['x-amz-algorithm'],
                'x-amz-date': clientPayload['x-amz-date'],
                'x-amz-credential': clientPayload['x-amz-credential'],
                'success_action_status': '201',
                'success_action_redirect': '',
                'file': {
                    filepath: fs.createReadStream(file.path),
                    filename: file.path,
                    contentType: null,
                }

            }),
            {
                headers:
                {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Accept-Charset': 'UTF-8'
                }
            }
        );

        return res;
    } catch (error) {
        if (axios.isAxiosError(error)) {
            logger.error('error message: ', error);
        } else {
            logger.error('unexpected error: ', error);
        }
    }
};

相关问题