-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadDirective.js
162 lines (127 loc) · 4.58 KB
/
uploadDirective.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
angular.module('myUploadDirective',[])
//factories
.factory('Cordova', function($q){
var d= $q.defer();
if(window.navigator){
d.resolve(window.navigator);
}
else{
document.addEventListener('deviceready', function(evt){;
d.resolve(navigator);
});
}
return{
navigator: function(){
return d.promise;
}
}
})
.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('fname', file);
return $http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
});
}; //check here
}])
.directive('fileUploadUrl', ['$parse', 'fileUpload', 'Cordova', function ($parse) {
function Controller( $scope, $element, $attrs, fileUpload, Cordova ) {
if(window.cordova)
$scope.isCordovaApp=true;
else
$scope.isCordovaApp=false;
//will execute when you click search (ng-click)
$scope.captureImage= function(){
//if cordova
if(window.cordova){
//on device ready
Cordova.navigator()
.then(function(navigator){
navigator.camera.getPicture(onSuccess, onFail, {quality: 50,
destinationType: Camera.DestinationType.FILE_URI
});
});//end then
//});//end listener
//clear camera cache
function clearCache(){
navigator.camera.cleanup();
}
var retries =0; //number of upload attempts
//now get image
function onSuccess(fileURI) {
//if upload is successful
var win = function (r) {
clearCache();
retries = 0;
alert("Done")
.then($scope.$apply($scope.callback(JSON.parse(r.response)) ));
}
// function(){ //display result on screen and number of results
// $scope.result= r.response.toString();
// $scope.resultLength= r.response.length;
// }
//if upload fails
var fail = function (error) {
if (retries == 0) {
retries ++
setTimeout(function() {
onSucess(fileURI)
}, 1000)
} else {
retries = 0;
clearCache();
alert('Something wrong happened!');
}//end else
}//end fail
//upload options
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1); //name of file
options.mimeType = "image/jpeg";
options.headers= [{'Content-Type': undefined}];
options.params = {}; // if we need to send parameters to the server request
var ft = new FileTransfer();
alert("Uploading");
ft.upload(fileURI, encodeURI($scope.uploadUrl), win, fail, options);
}//end on success
//if camera fails
function onFail(message) {
alert('Failed because: ' + message);
}
}//end if cordova view
//DESKTOP
//if desktop
else{
//upload photo to web
var file = $scope.myFile;
console.log('file is ' + JSON.stringify(file));
fileUpload.uploadFileToUrl(file, $scope.uploadUrl).success(function(data) {$scope.callback(data);})
.error(function(status){
alert("Error: "+status);});
} // end if desktop view
};//end capture image
}
return {
restrict: 'A',
controller:Controller,
scope:{uploadUrl:'@fileUploadUrl', callback:'=fileCallback'},
link: function(scope, element, attrs) {
// var model = $parse(attrs.fileModel);
// var modelSetter = model.assign;
if(window.cordova) {
element.attr("type","button");
element.attr("value","Search");
element.bind('click', scope.captureImage);
} else
element.bind('change', function(){
// scope.$apply(function(){
// modelSetter(scope, element[0].files[0]);
// });
scope.myFile= element[0].files[0];
scope.captureImage()
});
}
};
}]);