在ionic 中如何上传和下载文件,以及扫二维码等常见的功能
1.上传文件
(1).官方插件地址:https://github.com/apache/cordova-plugin-file-transfer
(2).安装方法:
[code lang="psd"] $ cordova plugin add cordova-plugin-file-transfer [/code]
(3).具体使用:
[code lang="js"] app.controller('UploadCtr', function($scope, $timeout, $cordovaFileTransfer) { $scope.uploadFile = function() { var url = "http://youraddress/uploads/"; //上传路径 //本地文件位置 var targetPath = "http://youripaddress/images/image.jpg"; //文件名 var filename = targetPath.split("/").pop(); var options = { fileKey: "file", fileName: filename, chunkedMode: false, mimeType: "image/jpg" }; $cordovaFileTransfer.upload(url, targetPath, options). then(function(result) { console.log("SUCCESS: " JSON.stringify(result.response)); alert("success"); alert(JSON.stringify(result.response)); }, function(err) { console.log("ERROR: " + JSON.stringify(err)); alert(JSON.stringify(err)); }, function (progress) { // constant progress updates $timeout(function () { $scope.downloadProgress = (progress.loaded / progress.total) * 100; }) }); }); [/code]
2.下载文件
(1).官方插件地址:https://github.com/apache/cordova-plugin-file
(2).安装方法:
[code lang="psd"] $ cordova plugin add cordova-plugin-file [/code]
(3).具体使用:
[code lang="js"] app.controller('DownloadCtr', function($scope, $timeout, $cordovaFileTransfer) { $scope.downloadFile = function() { var url = "http://your_ip_address/images/my.jpg"; var filename = url.split("/").pop(); alert(filename); var targetPath = cordova.file.externalRootDirectory + filename; var trustHosts = true var options = {}; //url提交的服务器地址 targetPath提交图片的本地地址 $cordovaFileTransfer.download(url, targetPath, options, trustHosts) .then(function(result) { // Success! alert(JSON.stringify(result));//把对象转化成字符串 }, function(error) { // Error alert(JSON.stringify(error)); }, function (progress) { $timeout(function () { $scope.downloadProgress = (progress.loaded / progress.total) * 100; }) }); } [/code]
3.扫描二维码
(1).官方插件地址:https://github.com/phonegap/phonegap-plugin-barcodescanner.git
(2).安装方法:
[code lang="psd"] $ cordova plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git [/code]
(3).具体使用:
[code lang="js"] app.controller('BarcodeScannerCtrl', function($scope, $timeout) { cordova.plugins.barcodeScanner.scan( function(result) { console.log('companys---', JSON.stringify($scope.companys)); if (result.cancelled) { $cordovaToast.show('取消扫描', 'long', 'center'); } else { $cordovaToast.show('扫描成功', 'long', 'center'); console.log('内容----' + JSON.stringify(result.text)); var str = "名称:"; var str1 = result.text; var objs = {}; objs.res = str1.split('\n')[2].replace("名称:", ''); if (objs.res) { $scope.gongsi = objs.res; $scope.companys[$scope.companys.length] = { enterpriseName: objs.res }; console.log("重新加载界面-2---"); console.log('model---', $scope.gongsi); console.log('companys---', JSON.stringify($scope.companys)); window.location.href = "#/tab/addDecisionBook" } } }, function(error) { console.log("扫描失败----- failed: " + error); // $cordovaToast.show('扫描失败请重新扫描', 'long', 'center'); } ); }); [/code]
详情参数请参考官网,具体需求具体处理,如有错误请及时留言。
官网地址:http://cordova.apache.org/docs/en/latest/