无法在“FormData”上执行“追加”:参数 2 不是“Blob”类型

我有一个允许用户上传多张图片的上传器。为了只创建或更新他想要添加/更改的图像,我更新了一个如下所示的对象:

{main: Blob, "1": Blob, "2":Blob}

所以如果后面只需要更新“1”,发送的对象只会包含 {"1": Blob}

单击保存时,它会触发一个函数,该函数应该将图像附加到 formData()。遗憾的是 formData 永远不会更新。我有以下错误:

无法在“FormData”上执行“追加”:参数 2 不是“Blob”类型。

export async function uploadImages(files, userId) {

  try {

    const images = new FormData();

    files.main && images.append("image", files.main, "main");

    files[1] && images.append("image", files[1], "1");

    files[2] && images.append("image", files[2], "2");


    const res = await ax.post(process.env.SERVER_URL + "/upload-images", {

      images,

      userId,

    });

    return "success"

  } catch (err) {

    return "error"

  }

}

如何解决这个问题?谢谢!



长风秋雁
浏览 230回答 4
4回答

HUX布斯

您不应该在控制台中看到 FormData 对象的内容,因为它不可序列化。您可以改为检查请求负载,检查浏览器开发工具中的“网络”选项卡,找到您的请求并查看“标头”选项卡底部以查看“FormData”日志。你会看到这样的东西:&nbsp;此外,您应该在 axios 中将标头“Content-Type”设置为“multipart/form-data”。这是工作示例:<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <meta charset="UTF-8" />&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0" />&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <input type="file" multiple id="filepicker" />&nbsp; &nbsp; <button id="send">Send</button>&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.20.0/axios.min.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; const myformData = new FormData();&nbsp; &nbsp; &nbsp; document&nbsp; &nbsp; &nbsp; &nbsp; .querySelector('#filepicker')&nbsp; &nbsp; &nbsp; &nbsp; .addEventListener('change', function (event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const { files } = event.target;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object.values(files).forEach(function (file, index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myformData.append(index, file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; document.querySelector('#send').addEventListener('click', function () {&nbsp; &nbsp; &nbsp; &nbsp; axios({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'http://google.com',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: myformData,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: { 'Content-Type': 'multipart/form-data' },&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then((response) => console.log(response))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .catch((err) => console.log(err));&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; </body></html>

收到一只叮咚

我在我的 React Native 应用程序上遇到了这个问题。为了解决它,我必须将图像路径转换为 blob。下面给出了我的句柄上传功能的代码。const handleUpload = async () => {&nbsp; &nbsp; if (selectedImage.localUri !== '') {&nbsp; &nbsp; &nbsp; &nbsp; const image_uri =&nbsp; Platform.OS === 'ios' ? selectedImage.localUri.replace('file://', '') : selectedImage.localUri;&nbsp; &nbsp; &nbsp; &nbsp; const response = await fetch(image_uri);&nbsp; &nbsp; &nbsp; &nbsp; const blob = await response.blob();&nbsp; &nbsp; &nbsp; &nbsp; const formData = new FormData();&nbsp; &nbsp; &nbsp; &nbsp; formData.append('image', blob, "xray_image.jpg");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; setLoading(true);&nbsp; &nbsp; &nbsp; &nbsp; axios.post("https://...", formData)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then((result) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .catch((err) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(err);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; console.log("Select a file error message");&nbsp; &nbsp; }};

LEATH

export async function uploadImages(files, userId) {&nbsp; try {&nbsp; &nbsp; const images = new FormData();&nbsp; &nbsp; for(const file of files){&nbsp; &nbsp; &nbsp; &nbsp; images.append("image", file);&nbsp; &nbsp; }&nbsp; &nbsp; const res = await ax.post(process.env.SERVER_URL + "/upload-images", {&nbsp; &nbsp; &nbsp; images,&nbsp; &nbsp; &nbsp; userId,&nbsp; &nbsp; });&nbsp; &nbsp; return "success"&nbsp; } catch (err) {&nbsp; &nbsp; return "error"&nbsp; }}

小唯快跑啊

您必须将扩展名与名称一起传递files[1]&nbsp;&&&nbsp;images.append("image",&nbsp;files[1],&nbsp;"custom-name.jpg");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript