upload.js
1.66 KB
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
var $ = require('yoho-jquery'),
handlebars = require('yoho-handlebars');
var tpl = '<div style="width: 0px; height: 0px; overflow: hidden;">' +
'<iframe id="yoho-upload-result" name="ajaxUpload" style="display:none"></iframe>' +
'<form id="yoho-upload" method="post" enctype="multipart/form-data" ' +
'action="/common/uploadImg" target="ajaxUpload">' +
'<input type="file" name="filename" class="file-name" />' +
'<input type="hidden" name="bucket" value="evidenceImages" />' +
'</form>' +
'</div>';
var uploadFn = handlebars.compile(tpl || '');
var $result,
$upload,
$filename;
var uping;
var num,
inter,
callback;
$('body').append(uploadFn({}));
$result = $('#yoho-upload-result');
$upload = $('#yoho-upload');
$filename = $upload.find('.file-name');
function up(opt) {
if (uping) {
return;
}
$filename.click();
if (opt && typeof opt.callback === 'function') {
callback = opt.callback;
} else {
callback = '';
}
}
function getResponse(cb) {
num = 0;
inter = setInterval(function() {
var res = $result[0].contentDocument.body.innerText || '';
if (num > 20 || res) {
uping = false;
$filename.val('');
clearInterval(inter);
}
num++;
if (res) {
$result[0].contentDocument.body.innerText = '';
if (cb) {
return cb($.parseJSON(res));
}
}
}, 500);
}
$filename.change(function() {
if ($(this).val()) {
uping = true;
$upload.submit();
getResponse(callback);
}
});
exports.up = up;