React Native上传图片

RN项目须要用到上传图片功能,整理一下作个记录。

插件选择是react-native-image-picker,还挺好用的,不过须要分ios和android不一样平台去配置. react

能够看看以前的文章https://segmentfault.com/a/11...android

如今给出git上的react-native-image-picker地址ios

当你经过react-native-image-picker获得本地图片信息时执行如下代码:

处理单张图片git

let formData = new FormData();//若是须要上传多张图片,须要遍历数组,把图片的路径数组放入formData中
 let file = {uri: response.uri, type: 'multipart/form-data', name: 'image.png'};   //这里的key(uri和type和name)不能改变,
 formData.append("files",file);   //这里的files就是后台须要的key

多张图片处理github

let formData = new FormData();json

for(var i = 0;i<imgAry.length;i++){
    let file = {uri: imgAry[i], type: 'multipart/form-data', name: 'image.png'};   
    formData.append("files",file);  
}

上传

uploadURL上传图片的地址segmentfault

fetch(uploadURL,{
            method:'POST',
            headers:{
                'Content-Type':'multipart/form-data',
            },
            body:formData,
        })
            .then((response) => response.json())
            .then((responseData)=>{
                 let  source = ret.data.images[0].oUrl
                     console.log(source)  //获得的uri(http格式)拿到后进行操做吧
              
            })
            .catch((error)=>{console.error('error',error)});

下面是我本身应用的例子

引入模块和上传地址
clipboard.pngreact-native

应用数组

clipboard.png

相关文章
相关标签/搜索