我正在尝试将文件(图像)从我的 native react 应用程序上传到后端服务器。 RESTFUL 后端服务器接受通过特定 url 上的 PUT 方法发送的文件。我坚持 react-native 试图找到通过 PUT 方法发送文件的正确方法。
我正在尝试复制
的行为
curl -X PUT -T "/path/to/file""http://myputserver.com/puturl.tmp"
我找到了 XMLHttpRequest method在浏览器上执行此操作但不适用于 react-native。有没有人经历过这个请帮忙!!!
最佳答案
fetch('https://mywebsite.com/endpoint/', {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
}),
})
.then((response) => response.json())
.then((responseJson) => {
alert(responseJson)
})
.catch((error) => {
console.error(error)
})
关于javascript - 在 react-native 中通过 PUT 方法上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380676/