[英]React-native-uploader Android Errors
我目前正在尝试调试一个react-native包( react-native-uploader ),该包用于尝试上传一捆文件(照片)。 尽管在ios上工作,但当前的实现针对android返回以下错误:
Response{protocol=http/1.1, code=405, message=Method Not Allowed, url=${config.apiBase}/load/${this.props.id}/uploadconfirmation}
错误是由软件包中的这一行引起的:
Response response = client.newCall(request).execute();
客户在哪里:
private final OkHttpClient client = new OkHttpClient()
请求在哪里:
Request{method=POST, url=${config.apiBase}/load/${this.props.id}/uploadconfirmation, tag=null}
我已经使用formdata成功地发布了到端点:
let tData = new FormData();
const that = this;
tData.append("confirmation_doc", {
uri: files[0].filepath,
type: "image/jpeg",
name: "confirmation_doc.jpg",
});
axios.post(
`${config.apiBase}/load/${this.props.id}/uploadconfirmation`,
tData
)
.then(response => {
Alert.alert(
"Success",
"Uploaded Successfully!",
[{ text: "OK", onPress: () => that.props.close() }],
{ cancelable: false }
);
});
我尝试查看源代码,以确定什么地方崩溃了,似乎一切都按预期发布(标题看起来不错,方法看起来不错,端点看起来不错)。 我对Java不太熟悉,因此不胜感激。
HTTP 405 Method Not Allowed ...是客户端错误。
在请求行中接收到的方法是源服务器已知的,但目标资源不支持。
如果JavaScript
可以运行,但是Java
无法运行...您可能正在寻找MultipartBuilder
...与MediaType.FORM
结合使用。
为了解决此问题,我不得不放弃了我一直在使用的react-native-uploader
软件包。 以下是我如何解决此问题的方法:
let tData = new FormData();
this.state.selectedImages.forEach((item, i) => {
tData.append("doc[]", {
uri: item.uri,
type: "image/jpeg",
name: item.filename || `filename${i}.jpg`,
});
});
fetch(`${config.apiBase}/load/${this.props.id}/uploadconfirmation`, {
method: "post",
headers: {
Accept: "application/x-www-form-urlencoded",
Authorization: `Token ${this.props.token}`,
},
body: tData,
})
.then(res => res.json())
.then(res => {
Alert.alert(
"Success",
"Uploaded Successfully!",
[{ text: "OK", onPress: () => that.props.close() }],
{ cancelable: false }
);
})
.catch(err => {
console.error("error uploading images: ", err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.