<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        react native實(shí)現(xiàn)往服務(wù)器上傳網(wǎng)絡(luò)圖片的實(shí)例

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:33:27
        文檔

        react native實(shí)現(xiàn)往服務(wù)器上傳網(wǎng)絡(luò)圖片的實(shí)例

        react native實(shí)現(xiàn)往服務(wù)器上傳網(wǎng)絡(luò)圖片的實(shí)例:如下所示: let common_url = 'http://192.168.1.1:8080/'; //服務(wù)器地址 let token = ''; //用戶登陸后返回的token /** * 使用fetch實(shí)現(xiàn)圖片上傳 * @param {string} url 接口地址 * @param {JSON} params body的
        推薦度:
        導(dǎo)讀react native實(shí)現(xiàn)往服務(wù)器上傳網(wǎng)絡(luò)圖片的實(shí)例:如下所示: let common_url = 'http://192.168.1.1:8080/'; //服務(wù)器地址 let token = ''; //用戶登陸后返回的token /** * 使用fetch實(shí)現(xiàn)圖片上傳 * @param {string} url 接口地址 * @param {JSON} params body的

        如下所示:

        let common_url = 'http://192.168.1.1:8080/'; //服務(wù)器地址
        let token = ''; //用戶登陸后返回的token
        /** 
         * 使用fetch實(shí)現(xiàn)圖片上傳
         * @param {string} url 接口地址
         * @param {JSON} params body的請(qǐng)求參數(shù)
         * @return 返回Promise 
         */
        function uploadImage(url,params){
         return new Promise(function (resolve, reject) {
         let formData = new FormData();
         for (var key in params){
         formData.append(key, params[key]);
         }
         let file = {uri: params.path, type: 'application/octet-stream', name: 'image.jpg'};
         formData.append("file", file);
         fetch(common_url + url, {
         method: 'POST',
         headers: {
         'Content-Type': 'multipart/form-data;charset=utf-8',
         "x-access-token": token,
         },
         body: formData,
         }).then((response) => response.json())
         .then((responseData)=> {
         console.log('uploadImage', responseData);
         resolve(responseData);
         })
         .catch((err)=> {
         console.log('err', err);
         reject(err);
         });
         });

        使用方法

        let params = {
         userId:'abc12345', //用戶id
         path:'file:///storage/emulated/0/Pictures/image.jpg' //本地文件地址
        }
        uploadImage('app/uploadFile',params )
         .then( res=>{
         //請(qǐng)求成功
         if(res.header.statusCode == 'success'){
         //這里設(shè)定服務(wù)器返回的header中statusCode為success時(shí)數(shù)據(jù)返回成功
         upLoadImgUrl = res.body.imgurl; //服務(wù)器返回的地址
         }else{
         //服務(wù)器返回異常,設(shè)定服務(wù)器返回的異常信息保存在 header.msgArray[0].desc
         console.log(res.header.msgArray[0].desc);
         }
         }).catch( err=>{ 
         //請(qǐng)求失敗
         })

        注意點(diǎn)

        let file = {uri: params.path, type: 'application/octet-stream', name: 'image.jpg'}中的type也可能是multipart/form-data
        formData.append("file", file)中的的file字段也可能是images

        普通網(wǎng)絡(luò)請(qǐng)求參數(shù)是JSON對(duì)象

        圖片上傳的請(qǐng)求參數(shù)使用的是formData對(duì)象

        總結(jié):

        React Native中雖然也內(nèi)置了XMLHttpRequest 網(wǎng)絡(luò)請(qǐng)求API(也就是俗稱的ajax),但XMLHttpRequest 是一個(gè)設(shè)計(jì)粗糙的 API,不符合職責(zé)分離的原則,配置和調(diào)用方式非常混亂,而且基于事件的異步模型寫起來(lái)也沒有現(xiàn)代的 Promise 友好。而Fetch 的出現(xiàn)就是為了解決 XHR 的問題,所以react Native官方推薦使用Fetch API。

        fetch請(qǐng)求示例如下:

        return fetch('http://facebook.github.io/react-native/movies.json')
         .then((response) => response.json())
         .then((responseJson) => {
         return responseJson.movies;
         })
         .catch((error) => {
         console.error(error);
         });

        使用Promise封裝fetch請(qǐng)求

        let common_url = 'http://192.168.1.1:8080/'; //服務(wù)器地址
        let token = ''; 
        /**
         * @param {string} url 接口地址
         * @param {string} method 請(qǐng)求方法:GET、POST,只能大寫
         * @param {JSON} [params=''] body的請(qǐng)求參數(shù),默認(rèn)為空
         * @return 返回Promise
         */
        function fetchRequest(url, method, params = ''){
         let header = {
         "Content-Type": "application/json;charset=UTF-8",
         "accesstoken":token //用戶登陸后返回的token,某些涉及用戶數(shù)據(jù)的接口需要在header中加上token
         };
         console.log('request url:',url,params); //打印請(qǐng)求參數(shù)
         if(params == ''){ //如果網(wǎng)絡(luò)請(qǐng)求中沒有參數(shù)
         return new Promise(function (resolve, reject) {
         fetch(common_url + url, {
         method: method,
         headers: header
         }).then((response) => response.json())
         .then((responseData) => {
         console.log('res:',url,responseData); //網(wǎng)絡(luò)請(qǐng)求成功返回的數(shù)據(jù)
         resolve(responseData);
         })
         .catch( (err) => {
         console.log('err:',url, err); //網(wǎng)絡(luò)請(qǐng)求失敗返回的數(shù)據(jù) 
         reject(err);
         });
         });
         }else{ //如果網(wǎng)絡(luò)請(qǐng)求中帶有參數(shù)
         return new Promise(function (resolve, reject) {
         fetch(common_url + url, {
         method: method,
         headers: header,
         body:JSON.stringify(params) //body參數(shù),通常需要轉(zhuǎn)換成字符串后服務(wù)器才能解析
         }).then((response) => response.json())
         .then((responseData) => {
         console.log('res:',url, responseData); //網(wǎng)絡(luò)請(qǐng)求成功返回的數(shù)據(jù)
         resolve(responseData);
         })
         .catch( (err) => {
         console.log('err:',url, err); //網(wǎng)絡(luò)請(qǐng)求失敗返回的數(shù)據(jù) 
         reject(err);
         });
         });
         }
        }

        使用fetch請(qǐng)求,如果服務(wù)器返回的中文出現(xiàn)了亂碼,則可以在服務(wù)器端設(shè)置如下代碼解決:

        produces="text/html;charset=UTF-8"

        fetchRequest使用如下:
        GET請(qǐng)求:
        fetchRequest('app/book','GET')
         .then( res=>{
         //請(qǐng)求成功
         if(res.header.statusCode == 'success'){
         //這里設(shè)定服務(wù)器返回的header中statusCode為success時(shí)數(shù)據(jù)返回成功
        
         }else{
         //服務(wù)器返回異常,設(shè)定服務(wù)器返回的異常信息保存在 header.msgArray[0].desc
         console.log(res.header.msgArray[0].desc);
         }
         }).catch( err=>{ 
         //請(qǐng)求失敗
         })
        
        POST請(qǐng)求:
        
        let params = {
         username:'admin',
         password:'123456'
        }
        fetchRequest('app/signin','POST',params)
         .then( res=>{
         //請(qǐng)求成功
         if(res.header.statusCode == 'success'){
         //這里設(shè)定服務(wù)器返回的header中statusCode為success時(shí)數(shù)據(jù)返回成功
        
         }else{
         //服務(wù)器返回異常,設(shè)定服務(wù)器返回的異常信息保存在 header.msgArray[0].desc 
         console.log(res.header.msgArray[0].desc);
         }
         }).catch( err=>{ 
         //請(qǐng)求失敗
         })

        fetch超時(shí)處理

        由于原生的Fetch API 并不支持timeout屬性,如果項(xiàng)目中需要控制fetch請(qǐng)求的超時(shí)時(shí)間,可以對(duì)fetch請(qǐng)求進(jìn)一步封裝實(shí)現(xiàn)timeout功能,代碼如下:

        fetchRequest超時(shí)處理封裝

        /**
         * 讓fetch也可以timeout
         * timeout不是請(qǐng)求連接超時(shí)的含義,它表示請(qǐng)求的response時(shí)間,包括請(qǐng)求的連接、服務(wù)器處理及服務(wù)器響應(yīng)回來(lái)的時(shí)間
         * fetch的timeout即使超時(shí)發(fā)生了,本次請(qǐng)求也不會(huì)被abort丟棄掉,它在后臺(tái)仍然會(huì)發(fā)送到服務(wù)器端,只是本次請(qǐng)求的響應(yīng)內(nèi)容被丟棄而已
         * @param {Promise} fetch_promise fetch請(qǐng)求返回的Promise
         * @param {number} [timeout=10000] 單位:毫秒,這里設(shè)置默認(rèn)超時(shí)時(shí)間為10秒
         * @return 返回Promise
         */
        function timeout_fetch(fetch_promise,timeout = 10000) {
         let timeout_fn = null; 
        
         //這是一個(gè)可以被reject的promise
         let timeout_promise = new Promise(function(resolve, reject) {
         timeout_fn = function() {
         reject('timeout promise');
         };
         });
        
         //這里使用Promise.race,以最快 resolve 或 reject 的結(jié)果來(lái)傳入后續(xù)綁定的回調(diào)
         let abortable_promise = Promise.race([
         fetch_promise,
         timeout_promise
         ]);
        
         setTimeout(function() {
         timeout_fn();
         }, timeout);
        
         return abortable_promise ;
        }
        
        let common_url = 'http://192.168.1.1:8080/'; //服務(wù)器地址
        let token = ''; 
        /**
         * @param {string} url 接口地址
         * @param {string} method 請(qǐng)求方法:GET、POST,只能大寫
         * @param {JSON} [params=''] body的請(qǐng)求參數(shù),默認(rèn)為空
         * @return 返回Promise
         */
        function fetchRequest(url, method, params = ''){
         let header = {
         "Content-Type": "application/json;charset=UTF-8",
         "accesstoken":token //用戶登陸后返回的token,某些涉及用戶數(shù)據(jù)的接口需要在header中加上token
         };
         console.log('request url:',url,params); //打印請(qǐng)求參數(shù)
         if(params == ''){ //如果網(wǎng)絡(luò)請(qǐng)求中沒有參數(shù)
         return new Promise(function (resolve, reject) {
         timeout_fetch(fetch(common_url + url, {
         method: method,
         headers: header
         })).then((response) => response.json())
         .then((responseData) => {
         console.log('res:',url,responseData); //網(wǎng)絡(luò)請(qǐng)求成功返回的數(shù)據(jù)
         resolve(responseData);
         })
         .catch( (err) => {
         console.log('err:',url, err); //網(wǎng)絡(luò)請(qǐng)求失敗返回的數(shù)據(jù) 
         reject(err);
         });
         });
         }else{ //如果網(wǎng)絡(luò)請(qǐng)求中帶有參數(shù)
         return new Promise(function (resolve, reject) {
         timeout_fetch(fetch(common_url + url, {
         method: method,
         headers: header,
         body:JSON.stringify(params) //body參數(shù),通常需要轉(zhuǎn)換成字符串后服務(wù)器才能解析
         })).then((response) => response.json())
         .then((responseData) => {
         console.log('res:',url, responseData); //網(wǎng)絡(luò)請(qǐng)求成功返回的數(shù)據(jù)
         resolve(responseData);
         })
         .catch( (err) => {
         console.log('err:',url, err); //網(wǎng)絡(luò)請(qǐng)求失敗返回的數(shù)據(jù) 
         reject(err);
         });
         });
         }
        }

        以上這篇react native實(shí)現(xiàn)往服務(wù)器上傳網(wǎng)絡(luò)圖片的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        react native實(shí)現(xiàn)往服務(wù)器上傳網(wǎng)絡(luò)圖片的實(shí)例

        react native實(shí)現(xiàn)往服務(wù)器上傳網(wǎng)絡(luò)圖片的實(shí)例:如下所示: let common_url = 'http://192.168.1.1:8080/'; //服務(wù)器地址 let token = ''; //用戶登陸后返回的token /** * 使用fetch實(shí)現(xiàn)圖片上傳 * @param {string} url 接口地址 * @param {JSON} params body的
        推薦度:
        標(biāo)簽: React native ReactNative
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 久久久高清日本道免费观看| 成人无遮挡毛片免费看| 在线观看人成视频免费无遮挡| 国产精品免费AV片在线观看| 国产精品亚洲二区在线观看| 亚洲欧洲国产成人精品| 国产精品亚洲天堂| 永久在线免费观看| 国产亚洲AV夜间福利香蕉149| 亚洲综合中文字幕无线码| a毛片在线还看免费网站| 亚洲乱码中文字幕综合234| 亚洲偷自精品三十六区| 无码人妻一区二区三区免费| 亚洲AV日韩AV永久无码免下载| 亚洲AV成人一区二区三区观看| 久久午夜伦鲁片免费无码| 亚洲精品线路一在线观看| 中文字幕乱码系列免费| 免费一级毛片在播放视频| 国产AV旡码专区亚洲AV苍井空| 久久99热精品免费观看动漫| 亚洲午夜日韩高清一区| 青柠影视在线观看免费高清| 综合亚洲伊人午夜网 | 国产精品亚洲精品久久精品| 毛片免费全部播放无码| 在线综合亚洲中文精品| 免费国内精品久久久久影院| 两个人看的www免费高清| 亚洲白色白色永久观看| 久久久高清日本道免费观看| 亚洲精品美女久久久久9999| 波多野结衣久久高清免费| 亚洲色偷偷偷综合网| 成年在线观看网站免费| 深夜久久AAAAA级毛片免费看| 国产成人精品免费视频软件| 亚洲丁香婷婷综合久久| 免费看无码自慰一区二区| 精品一区二区三区高清免费观看|