[英]How to upload an image file in multipart to the server in react-native?
[英]How to upload file to server in react native expo
我正在嘗試從我的本機應用程序將文件上傳到服務器。 我從使用ImagePicker
獲取文件,但無法將其上傳到服務器上。 我每次服務器都給我 400。任何幫助都會很棒。
這是我的代碼
我的配置文件.tsx
const handleProfileUpdate = async () => {
if (Platform.OS !== "web") {
const { status } = await ImagePicker.requestCameraRollPermissionsAsync();
if (status !== "granted") {
alert("Sorry, we need camera Permissions");
} else {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
//base64: true,
});
const localUri = result.uri;
console.log("ImagePIcker", localUri);
if (!result.cancelled) {
setProfileImage(localUri);
updateProfile(localUri);
}
}
}
};
用戶操作.ts
export const updateUserProfilePicture = (url: string) => async (
dispatch: any
) => {
var data = new FormData();
data.append("profilepic", url);
console.log("profileUrl", url);
const _rest = new restServices();
const token = await _rest.getAccessToken();
var config: AxiosRequestConfig = {
method: "put",
url: "http://xxxxxxxxxx/user/profilePic",
headers: {
Authorization: "Bearer " + token,
},
data: data,
};
axios(config)
.then((res) => {
console.log("profileUpdate", res);
})
.catch((err) => {
console.log("profileupdate", err);
});
};
您需要使用 contetType:multipart/form-data
這可能對你有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.