How to upload a file along with text using fetch in react native?

I'm trying to upload a file to the server using react-native-document-picker. The problem I'm facing is I don't know how to upload the file along with a text.In my app there is a portion for file upload also there is an area for writing some text.Then it will get uploaded to the server.So I've done the following.But I'm getting this error after submitting to server

unhandled promise rejection unsupported BodyInit type

sample code

 filepick = () => {
        DocumentPicker.show({
            filetype: [DocumentPickerUtil.allFiles()],
        }, (error, res) => {
        console.log(
            res.uri,
            res.type, // mime type
            res.fileName,
            res.fileSize
        );
        Alert.alert(res.fileName)
        Alert.alert
        this.setState({ file: res.fileName })

    });
};

onPressSubmit() {
fetch(GLOBAL.ASMNT_URL +${this.props.id}, {
method: ‘POST’,
headers: {
‘Accept’: “multipart/form-data”,
‘Content-Type’: ‘multipart/form-data’
},
body: ({
file: this.state.file,
comment: this.state.text
})

    }).then(response => response.json())
        .then(responseData => {
            this.setState({
                assgn: responseData

            });

            console.log(this.state.assgn);

        });
}

The function filepick() is called after choosing a file from your device.Please help me to find a solution.How do I upload this to server also how to send text without stringifying it?

#javascript #reactjs #react-native

5 Likes83.65 GEEK