react native 上传图片(后面有空写一个,完整的案例)

图片上传的思路很正确

https://www.jianshu.com/p/c9f030fa6754

1. 获取图片路径

利用react-native-image-crop-pick 从手机中获得图片的地址

使用它的原因,就是比react-native-image-pick支持的功能多一些。

2.  对图片尺寸进行压缩

使用 react-native-image-resizer  对图片进行尺寸上的压缩

4. 如何想要传给后台,还需要将base64,转成二进制文件形式,就需要 formData

React Native 0.62.* [TypeError: Network request failed] on file upload (正常上传文件时遇到的问题)

https://github.com/facebook/react-native/issues/28551

关于正则表达式的使用

1. JavaScript 的replace 的回调函数,获取标签字符串中,img标签src里面的值。

htmlText.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
        console.log("src里面的值",capture);
        let t = capture.replace(/data:image/jpeg;base64,/i, "");
console.log(
"去掉base64特殊符号的图片编码",t); srcArr.push(t); });
// /9j/4Ua/RXhpZgAATU0AKgAAAA

2. 取出标签字符串中,文本内容

 let textReg = new RegExp('<[^<>]+>','g');
 let textContent = htmlText.replace(textReg ,"");
//  console.log("正文内容 =",textContent);

3. 将标签字符串中的img属性src的值 ,替换掉,置空或者自己想要替代的东西(https://stackoverflow.com/questions/1298531/regular-expression-in-javascript-replace-image-src-attribute

root     = serviceURL("file") + "&src=" + encodeURIComponent(root);
// html itself
html     = html.replace(/src=['"](?:[^"'/]*/)*([^'"]+)['"]/g, "src='" + root + "/$1'");
// 
原文地址:https://www.cnblogs.com/tengyuxin/p/13367916.html