首页 > 解决方案 > 未处理的承诺拒绝:TypeError:网络请求失败的博览会文件上传

问题描述

我尝试将图片上传到远程服务器。我正在使用 Expo React Native。有我用来拍摄和上传图片的功能。

async function takeAndUploadPhotoAsync() {

let result = await ImagePicker.launchCameraAsync({
  allowsEditing: true,
  aspect: [4, 3],
});

if (result.cancelled) {
  return;
}


let localUri = result.uri;
let filename = localUri.split("/").pop();


let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;


let formData = new FormData();

formData.append("photo", { uri: localUri, name: filename, type });

return await fetch("https://rare-zebra-23.loca.lt/api", {
  mode: "no-cors", // no-cors, *cors, same-origin
  method: "POST",
  body: formData,
  headers: {
    "content-type": "multipart/form-data",
  },
});

我正在为我的服务器使用本地隧道。当我尝试获取时,我的 console.log [Unhandled promise reject: TypeError: Network request failed] 中有这个错误。GET 方法工作正常。我还使用常规的 Web 服务器检查了我的后端,它工作正常。而且世博会还有 FileSystem.uploadAsync() 方法,在我看来这也不起作用。也许有人可以解释为什么?或有一些工作示例。谢谢。

标签: node.jsreact-nativefetchexpo

解决方案


推荐阅读