$cordovaFileTransfer 拍照上传文件
来源:互联网 发布:java测试需要会什么 编辑:程序博客网 时间:2024/05/21 14:07
ionic开发项目,其中涉及到拍照上传文件,
按照官方的说法
第一步:配置
var options ={quality:50,//相片质量0-100destinationType:Camera.DestinationType.DATA_URL,// 返回类型:DATA_URL= 0,返回作为 base64 編碼字串。 FILE_URI=1sourceType:Camera.PictureSourceType.CAMERA,//从哪里选择图片:PHOTOLIBRARY=0,相机拍照=1,SAVEDPHOTOALBUM=2。0和1其实都是本地图库allowEdit:false,encodingType:Camera.EncodingType.JPEG,//保存的图片格式: JPEG = 0, PNG = 1targetWidth:200,targetHeight:200,mediaType:0,//可选媒体类型:圖片=0,只允许选择图片將返回指定DestinationType的参数。 // 視頻格式=1,允许选择视频,最终返回 FILE_URI。ALLMEDIA= 2,允许所有媒体类型的选择。cameraDirection:0,//枪后摄像头类型:Back= 0,Front-facing = 1popoverOptions:50,saveToPhotoAlbum:true,}
第二步:
$cordovaFileTransfer.upload(encodeURI(url), imgData, options).then(function (result) { console.log(JSON.stringify(result.response)); console.log("success");}, function (err) { console.log(JSON.stringify(err)); console.log("fail");}, function (progress) { // constant progress updates});
参数说明:遇到的坑,后台就是不能接收到文件和参数,不管怎样都为空
原因:saveToPhotoAlbum
这个参数的意思是拍照之后将图像保存到相册,如果为false,就不保存。如果你写的false,恭喜你,掉坑里了。
cordova插件拍照上传的时候,会传一个文件路径,如果你不保存到本地,上传的时候,插件就找不到文件,服务器就会收到一个空,附带的参数也会一并未空,原理就是这样。
demo:
app.config ={"ServiceRootPath":"http://blog.csdn.net/sixteen_cicle/","FileUploadPath":"FileServlet",}var ionLoading = function($ionicLoading, templateContent) {$ionicLoading.show({template : templateContent,duration : 2000});};
var setImg = function(){amarPic.getPic($scope).then(function(){//amarPic angular 服务名function(imagebase64){console.log(imagebase64); var options = new FileUploadOptions();//cordova标准 options.params = {"transCode": "salesman.app.uploadheadimg", "userid":"userid"}; options.fileKey = "imagebase64"; options.fileName = imagebase64;//传入图片文件 var url = encodeURI(AmApp.config.ServiceRootPath + AmApp.config.FileUploadPath);//uri编码// $cordovaFileTransfer上传文件 $cordovaFileTransfer.upload(url,imagebase64,options) .then(function(result){ // console.log(result); // JSON 字符串 - js变量 var result = JSON.parse(result.response); $ionicLoading.hide();//隐藏,移除的区别? if(result.issuccess) { ionLoading($ionicLoading, "图片上传成功"); $scope.info.imagesrc = imagebase64; }else{ ionLoading($ionicLoading, "图片上传失败"); } },function(err){ $ionicLoading.hide();//隐藏,移除的区别? ionLoading($ionicLoading, "图片上传失败");},function(progress){ $ionicLoading.show({ template : "正在处理中", });});}},);};
阅读全文
3 0
- $cordovaFileTransfer 拍照上传文件
- ionic 图片拍照;选择上传 ngcordova cordovaFileTransfer cordovaCamera
- 关于$cordovaFileTransfer上传文件和参数遇到的坑
- ionic+ngCordova+formData拍照上传多文件
- 拍照上传
- 拍照上传
- Android 拍照上传与选择文件上传至服务器
- android 拍照上传照片(MiME文件上传类库,实现文件上传)
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- 上传文件之获取拍照照片和本地图库
- android 通过webView上传文件给服务器(包括拍照)
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- Android:使用webview上传文件(支持相册和拍照) .
- android使用webview上传文件(支持相册和拍照)
- 微信小程序-拍照或选择图片并上传文件
- 微信小程序-拍照或选择图片并上传文件
- 高并发处理
- ScrollView中事件监听和GestureDetector手势监听冲突
- 小米手机无法调试应用解决Installation failed with message Failed to establish session.
- JEECMS自定义标签开发步骤
- jsp 重定向、定时刷新
- $cordovaFileTransfer 拍照上传文件
- WSAAsyncSelect之MFC模型
- CSS选择器
- 服务器被爆破
- java中对象的创建和销毁
- linux 查询目录下包含关键字的所有文件
- FastJson的一般使用
- picasso使用
- spring boot 两个war包同时在tomcat中部署后启动报错