js请求发送表单数据格式的参数(向后端直传文件时使用)

 

const formData = new FormData();
formData.append('file', file);

 讲数据处理成file:file的表单数据格式 

const handleUpload = async (file) => {
    const formData = new FormData();
    formData.append('file', file);
    
    try {
      const response = await axios.post('your-backend-endpoint', formData);
      // 处理后端返回的数据
      console.log(response.data);
      message.success('文件上传成功');
    } catch (error) {
      console.error('文件上传失败:', error);
      message.error('文件上传失败');
    }
  };

 

posted @ 2024-04-18 16:56  SimoonJia  阅读(32)  评论(0编辑  收藏  举报