Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Created November 6, 2017 13:13
Show Gist options
  • Save Sanix-Darker/a04f5d33fac2fc304e7d8e0b843fd1a4 to your computer and use it in GitHub Desktop.
Save Sanix-Darker/a04f5d33fac2fc304e7d8e0b843fd1a4 to your computer and use it in GitHub Desktop.
AngularJS IONIC upload file
<div ng-if="!profile_tab">
<div class="card-item" ng-click="changeAvatar('id_front')">
<img ng-if="user.individual_profile.national_id_front=='/images/missing.png'"
ng-src="img/auth/missing_card.png" alt="" class="row no-padding">
<img ng-if="user.individual_profile.national_id_front!='/images/missing.png'"
ng-src="{{user.individual_profile.national_id_front}}" alt="" class="row no-padding">
<div class="row peex-first-background text-center card-id">
<span class="text-center">
National ID Front
</span>
</div>
</div>
$scope.changeAvatar = function (target) {
$scope.hideSheet = $ionicActionSheet.show({
buttons: [
{ text: 'Take photo <i class="ion-camera"></i>' },
{ text: 'Photo from library <i class="ion-navigate"></i>' }
],
titleText: 'Change Avatar',
cancelText: 'Close',
buttonClicked: function(index) {
$scope.addImage(index,target);
}
});
}
$scope.addImage = function(type,target) {
$scope.hideSheet();
var params = {};
switch (target) {
case 'avatar':
params.uri = 'users/avatars/';
break;
case 'id_front':
params.uri = 'users/documents/national_id_front';
break;
case 'id_back':
params.uri = 'users/documents/national_id_back';
break;
case 'utility_bill':
params.uri = 'users/documents/utility_bill';
break;
}
ImageService.handleMediaDialog(type).then(function(d) {
$scope.profile= cordova.file.dataDirectory + FileService.last_image();
console.log(target, d);
switch (target) {
case 'avatar':
$scope.user.avatar= d;
break;
case 'id_front':
$scope.user.individual_profile.national_id_front= d;
break;
case 'id_back':
$scope.user.individual_profile.national_id_back= d;
break;
case 'utility_bill':
$scope.user.individual_profile.utility_bill= d;
break;
}
upload(d, params.uri).then(function (result) {
if (result.avatar)
$scope.userForm.avatar = result.avatar;
switch (target) {
case 'avatar':
ToastApi.success({msg: 'EDIT_PROFILE.AVATAR_CHANGE'});
break;
case 'id_front':
ToastApi.success({msg: 'EDIT_PROFILE.ID_FRONT_CHANGE'});
break;
case 'id_back':
ToastApi.success({msg: 'EDIT_PROFILE.ID_BACK_CHANGE'});
break;
case 'utility_bill':
ToastApi.success({msg: 'EDIT_PROFILE.UTILITY_BILL'});
break;
}
AuthFactory.updateUserInfo(result);
});
});
}
var upload = function (filePath,uri) {
var defer = $q.defer();
var url = BASE_URL.apiEndpoint + uri;
var options ={
headers: {
'Access-Token': AuthFactory.currentToken()
},
params:{
},
httpMethod: 'PUT'
}
$cordovaFileTransfer.upload(url, filePath, options)
.then(function(result) {
defer.resolve(result)
}, function(err) {
// Error
console.log(err)
defer.reject()
}, function (progress) {
// constant progress updates
console.log(progress)
});
return defer.promise
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment