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
|
/*
* Copyright (C) 2015 "IoT.bzh"
* Author "Fulup Ar Foll"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details..
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Using_hidden_file_input_elements_using_the_click%28%29_method
* https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
* https://www.terlici.com/2015/05/16/uploading-files-locally.html
* https://github.com/nervgh/angular-file-upload/blob/master/src/services/FileUploader.js
*/
function changeInput() {
console.log ('input imgClicked');
}
(function() {
'use strict';
// WARNING: Angular ng-change does not work on input/file. Let's hook our callback through standard JS function
var tmpl = '<form target="null" action="/api/afbs/file-upload" method="post" enctype="multipart/form-data" >'+
'<input type="file" name="{{name}}" onchange="angular.element(this).scope().UpLoadFile(this.files)" accept="{{mime}}/*" style="display" >'+
'<input type="submit" class="submit" style="display" > ' +
'</form>' +
'<img id="{{name}}-img" src="{{imagepath}}" ng-click="imgClicked()">' ;
function basename(path) {
return path.split('/').reverse()[0];
}
angular.module('UploadFile',['ConfigApp'])
.directive('uploadFile', function(ConfigApp, $http, JQemu) {
function mymethods(scope, elem, attrs) {
// get widget image handle from template
scope.imgElem = elem.find('img');
scope.inputElem = elem.find('input');
scope.submitElem = JQemu.findByType (elem.children(), "submit");
// Image was ckick let's simulate an input (file) click
scope.imgClicked = function () {
scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
};
// upload file to server
scope.UpLoadFile= function(files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.log ("Selected file=" + file.name + " size="+ file.size/1024);
var mimeType = /image.*/; // build regular expression from Mime
if (!file.type.match(mimeType)) {
continue;
}
if (file.size > scope.sizemax*1024) {
scope.imagepath = scope.istoobig; // warning is path is wrong nothing happen
scope.$apply('imagepath'); // we short-circuit Angular resync Image
} else {
scope.basename=basename(file.name);
scope.imgElem[0].file = file;
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (upload) {
scope.imagepath = upload.target.result;
scope.$apply('imagepath'); // we short-circuit Angular resync image
scope.submitElem[0].click(); // Warning Angular TriggerEvent does not work!!!
};
}
}
};
// Initiallize default values from attributes values
if (attrs.icon) scope.imagepath= ConfigApp.paths[attrs.category] + attrs.icon;
else scope.imagepath=ConfigApp.paths.avatars + 'tux-bzh.png';
if (attrs.istoobig) scope.istoobig= ConfigApp.paths[attrs.category] + attrs.istoobig;
else scope.istoobig=ConfigApp.paths.avatars + 'istoobig.jpg';
scope.name= attrs.name || 'avatar';
scope.mime= attrs.mime || 'image';
scope.sizemax= attrs.sizemax || 100; // default max size 100KB
}
return {
restrict: 'E',
template: tmpl,
link: mymethods,
scope: {
callback : '='
}
};
});
console.log ("UploadFile Loaded");
})();
|