cordova plugin add cordova-plugin-camera //用于通过相机、相册选择图片并完成裁剪 cordova plugin add cordova-plugin-file-transfer //用于上传图片到服务器
angular.module('starter.services', [])
//文件上传
.factory('UploadFile', function(Toast) {
return {
/**
* 上传文件到服务器
*
* @param fileUrl 文件路径
* @param server 服务器接口
*/
uploadFile: function(fileUrl, server) {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var options = new FileUploadOptions();
options.fileKey = "BeanYon";
options.fileName = fileUrl.substr(fileUrl.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
options.chunkedMode = false;
var params = {account: localStorage.account};
options.params = params;
var ft = new FileTransfer();
ft.upload(fileUrl,
encodeURI(server),
success,
err,
options);
}
function success(r){
Toast.show("设置头像成功");
}
function err(error){
Toast.show("上传头像失败,请确保网络正常后再试");
}
}
}
})
//配置单张图片选择
.factory('SelectPicture', function(UploadFile, Toast) {
return {
/**
* 从相机或图库选择一张图片
*
* @param type 选择类型,0 拍照,1 相册
* @param width 目标宽度
* @param height 目标高度
* @param scope $scope对象
*/
chooseSinglePicture: function(type, width, height, scope) {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var options = {//相机配置
targetWidth: width,
targetHeight: height,
quality: 100,
allowEdit: true
}
if(type == 1){//图片源设置为相册
options.sourceType = 2;
}
navigator.camera.getPicture(
function(res){
scope.avatar_src = res;
scope.$apply();
localStorage.avatar = res;
UploadFile.uploadFile(res, "我的服务器地址");//传递自己的服务器接口地址
}, function(res){
Toast.show("选择头像失败");
}, options
);
}
},
/**
* 从图库选择多张图片
*/
choosePictures: function() {
window.imagePicker.getPictures(function(res){
alert(res+",success");
}, function(res){
alert(res+",failed");
}, {
maximumImagesCount: 10,
width: 80,
height: 80,
quality: 80
});
}
}
});
angular.module('starter.controllers', [])
.controller('MyCtrl', function($scope, $state, $ionicActionSheet, UploadFile,Toast, SelectPicture) {
$scope.avatar_src = "img/default_avatar.jpg";
/**
*选择头像
*/
$scope.selectAvatar = function(){
// 显示操作表
$ionicActionSheet.show({
buttons: [
{ text: '<p style="font-size: 18px;">拍照<p>' },
{ text: '<p style="font-size: 18px;">从相册选择<p>' },
],
buttonClicked: function(index) {
//设置头像
SelectPicture.chooseSinglePicture(index, 120, 120, $scope);
return true;
}
});
}
})
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有