英文:
What is the correct way to use Axios Create With Multipart and FormData in React Native
问题
I want to separate the config and the post from axios with react native using multipart upload.
Example:
let param = {file: fileObject.uri, mimeType: fileObject.mimeType, name: fileObject.name, title: text}
ApiConfigAxios.js
export const MultiPartAxios = (formData) => {
return ApiConfigMultiPartAxios = axios.create({
baseURL: website,
withCredentials: true,
data: formData,
headers: {
"Accept": "application/json",
'Content-Type': 'multipart/form-data',
"Authorization": `Basic ${base64data}`,
},
});
}
ApiAxios.js
export const ApiUpload = async (param) => {
const formData = new FormData();
formData.append('data', param);
if (param.case === 1) {
const response = await MultiPartAxios(formData).post('/ApiUpload');
return response.data;
}
};
But I got an error:
Object {
"error": "Request failed with status code 500",
}
undefined
What is the correct way to do it?
英文:
I want to separate the config and the post from axios with react native using multipart upload.
Example :
let param = {file: fileObject.uri, mimeType: fileObject.mimeType, name: fileObject.name, title: text}
ApiConfigAxios.js
export const MultiPartAxios = (formData) => {
return ApiConfigMultiPartAxios = axios.create({
baseURL: website,
withCredentials: true,
data: formData,
headers: {
"Accept": "application/json",
'Content-Type': 'multipart/form-data',
"Authorization": `Basic ${base64data}`,
},
});
}
ApiAxios.js
export const ApiUpload = async (param) => {
const formData = new FormData();
formData.append('data', param);
if (param.case === 1) {
const response = await MultiPartAxios(formData).post('/ApiUpload');
return response.data;
}
};
But I got error :
Object {
"error": "Request failed with status code 500",
}
undefined
What is the correct way to do it ?
答案1
得分: 0
发现解决方案。
正确的做法是这个例子:
ApiConfigAxios.js
export const ApiConfigMultiPartAxios = axios.create({
baseURL: website,
withCredentials: true,
headers: {
"Accept": "application/json",
'Content-Type': 'multipart/form-data',
"Authorization": `Basic ${base64data}`,
},
});
ApiAxios.js
export const ApiUpload = async (param) => {
const formData = new FormData();
formData.append('file', {
uri: param.file,
type: param.mimeType,
name: param.name,
});
formData.append('title', param.title);
formData.append('case', param.case);
if (param.case === 1) {
const response = await ApiConfigMultiPartAxios.post('/ApiUpload', formData);
return response.data;
}
};
英文:
Found the solution.
The correct way to do it is this example :
ApiConfigAxios.js
export const ApiConfigMultiPartAxios = axios.create({
baseURL: website,
withCredentials: true,
headers: {
"Accept": "application/json",
'Content-Type': 'multipart/form-data',
"Authorization": `Basic ${base64data}`,
},
});
ApiAxios.js
export const ApiUpload = async (param) => {
const formData = new FormData();
formData.append('file', {
uri: param.file,
type: param.mimeType,
name: param.name,
});
formData.append('title', param.title);
formData.append('case', param.case);
if (param.case === 1) {
const response = await ApiConfigMultiPartAxios.post('/ApiUpload', formData);
return response.data;
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论