我正在向服务器发出一个POST请求,以上传图像,并使用axios在react本机中发送格式数据。我收到了“网络错误”。我也尝试了获取,但是没有work.using反应本地图像选择器libeary为选择image.in邮递员api工作良好。
formData.append('title', Title);
formData.append('class_id', selectClass._id)
formData.append('subject_id', checkSelected)
formData.append('teacher_id', userId)
formData.append('description', lecture);
formData.append('type', 'image');
var arr=[];
arr.push(imageSource)
arr.map((file,index)=>{
formData.append('file',{
uri:file.path,
type:file.type,
name:file.name
})
})
axios({
method: 'post',
url: URL + 'admin/assignment/create',
data: data,
headers: {
"content-type": "multipart/form-data",
'x-auth-token': token,
},
})
.then(function (response) {
//handle success
console.log('axios assigment post',response);
})
.catch(function (response) {
//handle error
console.log('axios assigment post',response);
});发布于 2020-04-17 04:31:04
“反应-原生”:"0.62.1",“反应”:"16.11.0",“公理”:"^0.19.2",
奇怪的解决方案我必须删除android ->app->source->调试中的调试文件夹。
并重新启动应用程序,它解决了我的问题。我觉得这是缓存问题。
发布于 2020-07-01 14:34:42
项目将flipper java文件保存在app > source > debug下,在react本机> 0.62下。Flipper网络有一个问题,在您的情况下会导致这个问题。如果删除debug文件夹,您将无法使用Flipper调试Android,因此最好的解决方案是将android > gradle.properties中的Flipper版本升级到0.46.0,从而解决这个问题。
您可以用这行FLIPPER_VERSION=0.46.0来更改它
反应本土化安卓
发布于 2021-03-29 04:40:07
我所面临的问题是,当我使用图像选择器并试图使用axios上传文件时,我得到了NetworkError。它在iOS上运行得很好,但在安卓系统中却不起作用。
我就是这样解决这个问题的。
这里有两个独立的问题。假设我们从图像选择器获得imageUri,那么我们将使用以下代码行从前端上传。
const formData = new FormData();
formData.append('image', {
uri : imageUri,
type: "image",
name: imageUri.split("/").pop()
});第一个问题是imageUri本身。如果假设照片路径是/user/./ path /to/file.jpg。然后,android中的文件选择器将imageUri值作为文件:/ imageUri /./path/to/file.jpg,而iOS中的文件选择器则将imageUri值作为iOS
第一个问题的解决方案是在android的formData中使用file://而不是file:。
第二个问题是,我们没有使用适当的mime类型。它在iOS上运行得很好,但在安卓系统上就不行了。更糟糕的是,文件选择程序包将文件类型作为“映像”,而不提供适当的mime类型。
解决方案是在字段类型中的formData中使用适当的mime类型。示例:用于.jpg文件的mime类型为image/jpeg,对于.png文件的mime类型为image/png。我们不需要手工操作。相反,您可以使用一个非常著名的npm包,名为哑剧。
最后的工作解决办法是:
import mime from "mime";
const newImageUri = "file:///" + imageUri.split("file:/").join("");
const formData = new FormData();
formData.append('image', {
uri : newImageUri,
type: mime.getType(newImageUri),
name: newImageUri.split("/").pop()
});我希望这有助于解决你的问题:)
https://stackoverflow.com/questions/61175557
复制相似问题