简体   繁体   English

FormData 无法与 Expo CLI React native 一起正常工作

[英]FormData is not working fine with Expo CLI React native

recently I got an issue with uploading documents functionality in Expo CLI - React native, the issue is the file is not delivering to the backend like FormData() it arrives in a weird array, not like the website at all, I got the same endpoint connected to React JS project and it works fine and the browser delivers the documents correctly, But not sure what is wrong with FromData here, I also tried react-native-fs but it's not compatible with Expo and here is my code最近我在 Expo CLI - React native 中遇到上传文档功能的问题,问题是文件没有像 FormData() 那样传送到后端,它以一个奇怪的数组到达,根本不像网站,我有相同的端点连接到 React JS 项目,它工作正常,浏览器正确传送文档,但不确定这里的 FromData 有什么问题,我也尝试过 react-native-fs 但它与 Expo 不兼容,这是我的代码

                    let formData = new FormData();
                formData.append('file', doc);
                let token = this.props.user.token;
                let header = { headers: { 'Accept': '*', 'Authorization': 'Bearer '+token, 'Content-Type': 'multipart/form-data' } };
                let res = await axios.post(BackendURL+'/porta/files/upload/'+document.projectDocumentId, formData, header);
                if(res.data.success === true){
                    alert('File uploaded successfully');
                    this.props.ReloadData(true);
                }
                console.log(doc);

Issue has been solved, the problem where in the header, i removed the content type & accept like this问题已解决,问题是在 header 中,我删除了内容类型并像这样接受

                    let formData = new FormData();
                formData.append('file', {
                    name: doc.name,
                    uri: doc.uri,
                    type: "application/pdf"
                });
                this.props.loadingStatus(true); 
                let token = this.props.user.token;
                let header = { headers: {  'Authorization': 'Bearer '+token } };
                let res = await axios.post(BackendURL+'/portal/files/upload/'+document.projectDocumentId, formData, header);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM