0

我有个问题。我在 React 中创建了一个文件上传。不幸的是,我只得到了 blob url,例如blob = "blob:http://localhost:3000/image"。但它没有用。在 Postman 中,我可以直接为文件创建一些东西。如何将我的 blob 创建到正确的 img,后端可以处理它?后端需要一个缓冲区。我得到了我的代码TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed.如何将 blob url 创建到 Buffer 数组以便后端可以处理它?

我尝试什么:

不幸的是有一个错误

// This is my URL
blob = "blob:http://localhost:3000/image"

const blobToBase64 = blob => {
  const reader = new FileReader();
  reader.readAsDataURL(blob);
  return new Promise(resolve => {
    reader.onloadend = () => {
      resolve(reader.result);
    };
  });
};


var base64; 
blobToBase64(blob).then(res => {
  // do what you wanna do
  console.log(res); // res is base64 now
  base64 = res;
});

data = base64;

axios
      .post("http://localhost:4000/upload/", {
        data: data,
      })
      .then((res) => {
        if (res.status === 200) {
          console.log("WORKS IMAGE")
        }
      })
      .catch((error) => {
        console.log(error);
      });
  };

邮递员查询

    var axios = require('axios');
    var FormData = require('form-data');
    var fs = require('fs');
    var data = new FormData();
    data.append('image', fs.createReadStream('/C:/Kazim/test.png'));
    
    var config = {
      method: 'post',
      url: 'localhost:4000/upload/',
      headers: { },
      data : data
    };
    
    axios(config)
    .then(function (response) {
      console.log(JSON.stringify(response.data));
    })
    .catch(function (error) {
      console.log(error);
    });

我的缓冲区应该是这样的

{
  image: {
    name: 'test.png',
    data: <Buffer 89 124e 47 0d 0a 1a 0a 58 00 00 0d 58 48 44 52 00 00 07 ce 00 00 08 ce 08 02 11 00 11 e3 a9 f1 77 12 00 00 01 73 52 58 42 00 ae ce 1c e9 00 00 00 
more bytes>,
    size: 21220,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'image/png',
    md5: '2f54581b868376d15d5fcaf63d',
    mv: [Function: mv]
  }
}
4

0 回答 0