RN-第三方-react-native-image-picker,选择图片上传

来源:互联网 发布:vb中picturebox的文本 编辑:程序博客网 时间:2024/06/05 18:36

https://github.com/react-community/react-native-image-picker

npm install react-native-image-picker@latest --save
react-native link

具体代码参考demo里面的APP.js


QQ20170621-162902.gif

可以设置直接访问相机、相册,也可以弹出选择框进行选择


87273454-3112-445A-9575-FD62A21A43D3.png

选择图片后的图片信息


F2434686-77A5-43F2-948D-35375BF4FEF4.png

处理上传数据,单张图片

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

多张图片上传

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

上传

 fetch(uploadURL,{        method:'POST',        headers:{            'Content-Type':'multipart/form-data',        },        body:formData,    })        .then((response) => response.text() )        .then((responseData)=>{            console.log('responseData',responseData);        })        .catch((error)=>{console.error('error',error)});
阅读全文
0 0
原创粉丝点击