使用获取呼叫和获取网络请求上传图像失败
我正在尝试使用fetch呼叫bellow上传图像,并且我会遇到以下错误:
Network request failed
at node_modules\whatwg-fetch\dist\fetch.umd.js:535:17 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:123:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:379:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:113:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:365:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:4 in callFunctionReturnFlushedQueue
当我从FormData中删除文件时,它可以正常工作。我在这里阅读了所有相关帖子,对我没有任何帮助。我尝试使用Axios上传文件,但是由于某些FormData-Axios对较新的React-native版本不兼容,但这也不起作用。 URI不是问题,原因是我要在Android上进行测试,而不是在iOS上进行测试,而您必须删除您“ file://”。
const data = new FormData();
data.append("file", {
uri: result.uri,
type: "image/jpg",
name: "photo.jpg",
});
data.append("user", "test");
res = await fetch("https://modern-zebra-85.loca.lt/api/process", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "multipart/form-data",
},
body: data,
})
.then((result) => console.log(result))
.catch((err) => console.log(err));
I'm trying to upload an image using the fetch call bellow and I keep getting the following error:
Network request failed
at node_modules\whatwg-fetch\dist\fetch.umd.js:535:17 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:123:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:379:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:113:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:365:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:4 in callFunctionReturnFlushedQueue
When I remove the file from the formData it works fine. I've read all the relevant posts on here and nothing works for me. I've tried uploading the file using axios but that didn't work either because of some formdata-axios incompatibilities for the newer react-native versions. The uri is not the problem cause I'm testing on android and not on ios where thee "file://" would have to be removed.
const data = new FormData();
data.append("file", {
uri: result.uri,
type: "image/jpg",
name: "photo.jpg",
});
data.append("user", "test");
res = await fetch("https://modern-zebra-85.loca.lt/api/process", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "multipart/form-data",
},
body: data,
})
.then((result) => console.log(result))
.catch((err) => console.log(err));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了它,我正在使用LocalTunnel来使用公共URL运行应用程序并在本地托管服务器(以便我可以访问结果),并且可能LocalTunnel可能不会传输文件。我认为这是因为一旦我尝试在服务器的实时版本上发布一切正常。
Solved it, I was using localtunnel to be able to run the app using a public url and hosting the server locally (so that I could access the results) and probably localtunnel doesn't transfer files. I assume that because once I tried posting on the live version of my server everything worked fine.