简体   繁体   中英

How to upload an image file in multipart to the server in react-native?

I am developing an app in react-native .
I am choosing an image from the image picker in the Android device and want to upload this file in multipart to the server in a POST request.

You should create a form data and push the files that you want to send to your api.

                       const sendableFormData = new FormData();

                        photos.map((element: any, index: number) => {
                            const imageUrl: any = {
                                uri: element.uri,
                                type: element.mime,
                                name: `imageFilename-${index}`,
                            };
                       sendableFormData.append('imageUrls', imageUrl, `imageFilename-${index}`);

axios(
            {
                method: 'post',
                url: `url`,
                data: sendableFormData,
                headers: { 'Content-Type': 'multipart/form-data' },
            },
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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