在本机反应中上传文件

oro*_*hen 5 file react-native

如何以最简单的方式将react-native中的文件上传到服务器?网上的信息和图书馆太多,因此我不知道它们是否可以安全使用。

谢谢。

Jay*_*mar 5

这是使用将文件上传到服务器的方法fetch

        const body = new FormData();

        //String Key value pair appending to body   
        body.append('KEY', VALUE);
        body.append('KEY', VALUE);
        body.append('KEY', VALUE);

        //Appending file to body
        body.append(KEY_AS REQUIRED_IN_SERVICE, {
                uri: PASS_URI_OF_THE_FILE,
                type: 'image/jpeg', //This is the file type .. you can define according to your requirement
                name: 'photo.jpg',   //File name you want to pass
            })
        //Service call                        
        fetch(YOUR_URL, {
            method: 'POST',
            headers: new Headers({
                YOUR_HEADER_PARAMS
            }),
            body: body
        })
            .then(res => res.json())
            .then((responseJson) => {

               //GET RESPONSE SUCCESS OF FAILURE

            })
            .catch((error) => {
               //ERROR 
            });
    }
Run Code Online (Sandbox Code Playgroud)