Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js React Native使用express和multer通过fetch将多个图像上传到node js服务器_Node.js_Image_React Native_Fetch_Multer - Fatal编程技术网

Node.js React Native使用express和multer通过fetch将多个图像上传到node js服务器

Node.js React Native使用express和multer通过fetch将多个图像上传到node js服务器,node.js,image,react-native,fetch,multer,Node.js,Image,React Native,Fetch,Multer,我正在尝试将多个图像上载到我的nodejs服务器,在那里我使用multer解析表单数据, 但问题是,当我试图通过postman上传图像时,它成功上传,但使用react native fetch api时,它不起作用 这是我的密码: 反应本机: import React from "react"; import { View, Text, Button } from "react-native"; export default function imageUpload(props) {

我正在尝试将多个图像上载到我的nodejs服务器,在那里我使用multer解析表单数据, 但问题是,当我试图通过postman上传图像时,它成功上传,但使用react native fetch api时,它不起作用

这是我的密码:

反应本机:

import React from "react";
import { View, Text, Button } from "react-native";

export default function imageUpload(props) {



 function upload() {
    const photo1 = {
      uri: "https://html5box.com/html5gallery/images/Swan_1024.jpg",
      type: "image/jpeg",
      name: "photo1.jpg",
    };
    const photo2 = {
      uri:
        "https://www.canva.com/wp-content/themes/canvaabout/img/colorPalette/image4.jpg",
      type: "image/jpeg",
      name: "photo2.jpg",
    };

    const photos = [photo1, photo2];
    const form = new FormData();

    for (let photo in photos) {
      form.append("image", photos[photo]);
    }

    console.log(form);

    fetch("http://192.168.31.208:3000/uploads", {
      body: form,
      method: "POST",
      headers: {
        "Content-Type": "multipart/form-data",
        // Accept: "application/x-www-form-urlencoded",
      },
    })
      .then((response) => response.json())
      .then((responseData) => {
        console.log("Response:" + responseData);
      })
      .catch((error) => {
        console.log(error);
      });
  }
  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Text>Image Upload</Text>
      <Button title="Test" onPress={upload} />
    </View>
  );
}
控制台输出前端:

    FormData {
  "_parts": Array [
    Array [
      "image",
      Object {
        "name": "photo1.jpg",
        "type": "image/jpeg",
        "uri": "https://html5box.com/html5gallery/images/Swan_1024.jpg",
      },
    ],
    Array [
      "image",
      Object {
        "name": "photo2.jpg",
        "type": "image/jpeg",
        "uri": "https://www.canva.com/wp-content/themes/canvaabout/img/colorPalette/image4.jpg",
      },
    ],
  ],
}

Network request failed
- node_modules\whatwg-fetch\dist\fetch.umd.js:473:29 in xhr.onerror
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

感谢您的帮助

如果您试图从手机(Android/iOS)上传图像,则无法将其作为URL传递。您应该发送一个本地文件路径/uri

如果您试图从移动设备(Android/iOS)上传图像,则无法将其作为URL传递。你应该发送一个本地文件路径/uri

实际上这只是一个示例,我正在传递拾取的图像的路径,即图像的确切本地地址,它确实适用于单个图像,但我的问题是,我想用一个获取请求上载所有图像,但实际上这只是示例,我正在传递拾取图像的路径,即图像的确切本地地址,它确实适用于单个图像,但我的问题是,我希望通过一个获取请求上载所有图像,但这不会发生
    FormData {
  "_parts": Array [
    Array [
      "image",
      Object {
        "name": "photo1.jpg",
        "type": "image/jpeg",
        "uri": "https://html5box.com/html5gallery/images/Swan_1024.jpg",
      },
    ],
    Array [
      "image",
      Object {
        "name": "photo2.jpg",
        "type": "image/jpeg",
        "uri": "https://www.canva.com/wp-content/themes/canvaabout/img/colorPalette/image4.jpg",
      },
    ],
  ],
}

Network request failed
- node_modules\whatwg-fetch\dist\fetch.umd.js:473:29 in xhr.onerror
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue