我怎样才能 POST 一个图像到 DB 通过 react native 和 fetch API?
How can I POST an image to DB via react native with the fetch API?
所以我正在尝试 POST 通过 React Native 和获取 API.
将图像发送到服务器
fetch(`${API}/uploadAvatar`, {
method: "POST",
headers: {
Authorization: bearer,
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
},
body: JSON.stringify({ file: result.uri }),
})
.then((response) => response.json())
.then((json) => {
console.log({ json });
// this console.log outputs:
// "The format of the file should be jpg, png, jpeg.",
})
.catch((err) => {
console.log({ err });
});
}
result
returns这个:
{
"cancelled": false,
"height": 1776,
"type": "image",
"uri": "file:///var/mobile/Containers/Data/Application/18F84F29-CB72-4615-A68F-A00422D9B119/Library/Caches/ExponentExperienceData/%2540heythere%252Fkeep-up/ImagePicker/959E8BDE-FCF4-40C6-AF18-8F9EA852760D.jpg",
"width": 1776,
}
这些是 POSTMAN 上的调用,您可以在其中看到它们的工作。
我做错了什么?
你的 postman 表明你正在使用表单数据上传图片,但在你的代码中你只是进行了 JSON post 调用而没有发送任何表单数据。您需要创建一个新的 FormData
实例,并将数据附加到它。在你的例子中,你想发送带有密钥 file
的 result.uri
,这可以使用 formData.append('file', result.uri)
来完成。然后你必须发送 formData 实例作为你的正文(方法为 POST,在你的情况下)
let formData = new FormData();
formData.append('file', result.uri);
fetch("api/SampleData", {
body: formData,
method: "post"
}).then((response) => response.json())
.then((json) => {
console.log({
json
});
})
.catch((err) => {
console.log({
err
});
});
您可以 post 在表单数据的帮助下将图像发送到服务器,方法是创建文件路径、文件名和文件类型的 JSON 对象,并将该对象附加到表单数据中带参数的实例。该文件的路径是特定于平台的,因此您必须为路径添加条件。请参考代码片段。
let Data = new FormData();
Data.append('file',
{
uri: Platform.OS === 'android' ? result.uri: result.uri.replace('file://',''),
type: result.type,
name: result.uri.replace(/^.*[\\/]/, '')
});
fetch("api/SampleData", {
body: Data,
method: "post",
headers: {'Content-Type': 'multipart/form-data'}
}).then((response) => response.json())
.then((json) => {
console.log({
json
});
})
.catch((err) => {
console.log({
err
});
});
所以我正在尝试 POST 通过 React Native 和获取 API.
将图像发送到服务器 fetch(`${API}/uploadAvatar`, {
method: "POST",
headers: {
Authorization: bearer,
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
},
body: JSON.stringify({ file: result.uri }),
})
.then((response) => response.json())
.then((json) => {
console.log({ json });
// this console.log outputs:
// "The format of the file should be jpg, png, jpeg.",
})
.catch((err) => {
console.log({ err });
});
}
result
returns这个:
{
"cancelled": false,
"height": 1776,
"type": "image",
"uri": "file:///var/mobile/Containers/Data/Application/18F84F29-CB72-4615-A68F-A00422D9B119/Library/Caches/ExponentExperienceData/%2540heythere%252Fkeep-up/ImagePicker/959E8BDE-FCF4-40C6-AF18-8F9EA852760D.jpg",
"width": 1776,
}
这些是 POSTMAN 上的调用,您可以在其中看到它们的工作。
我做错了什么?
你的 postman 表明你正在使用表单数据上传图片,但在你的代码中你只是进行了 JSON post 调用而没有发送任何表单数据。您需要创建一个新的 FormData
实例,并将数据附加到它。在你的例子中,你想发送带有密钥 file
的 result.uri
,这可以使用 formData.append('file', result.uri)
来完成。然后你必须发送 formData 实例作为你的正文(方法为 POST,在你的情况下)
let formData = new FormData();
formData.append('file', result.uri);
fetch("api/SampleData", {
body: formData,
method: "post"
}).then((response) => response.json())
.then((json) => {
console.log({
json
});
})
.catch((err) => {
console.log({
err
});
});
您可以 post 在表单数据的帮助下将图像发送到服务器,方法是创建文件路径、文件名和文件类型的 JSON 对象,并将该对象附加到表单数据中带参数的实例。该文件的路径是特定于平台的,因此您必须为路径添加条件。请参考代码片段。
let Data = new FormData();
Data.append('file',
{
uri: Platform.OS === 'android' ? result.uri: result.uri.replace('file://',''),
type: result.type,
name: result.uri.replace(/^.*[\\/]/, '')
});
fetch("api/SampleData", {
body: Data,
method: "post",
headers: {'Content-Type': 'multipart/form-data'}
}).then((response) => response.json())
.then((json) => {
console.log({
json
});
})
.catch((err) => {
console.log({
err
});
});