-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
78 lines (70 loc) · 2.11 KB
/
index.html
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
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>file upload example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.5.0/bluebird.min.js"></script>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script src="file-dialog.js"></script>
<style type="text/css">
#hidden-file { display: none; }
.nope { text-decoration: line-through; color: red; }
</style>
</head>
<body>
<p>
<button id="btn-1">upload csv</button>
<label for="btn-1">formData + ajax post (ie10+)</label>
</p>
<p>
<button id="btn-2">upload csv</button>
<label for="btn-2">fileReader api (ie10+)</label>
</p>
<div id="result"></div>
<pre id="metadata"></pre>
<script type="text/javascript">
// the values we have in IE (this is not a html5 file upload, so the content is not here of course)
function dumpMeta(file) {
$('#metadata').html(JSON.stringify({ name: file.name, size: file.size, type: file.type }, null, ' '));
}
$('#btn-1').on('click', function () {
fileDialog({ accept: '.csv' })
.then(function (file) {
var data = new FormData();
data.append('file', file[0]);
data.append('fileName', file[0].name);
dumpMeta(file[0]);
$.ajax({
url: 'index.html',
data: data,
processData: false,
contentType: false, // do NOT set contentType or mimeType
type: 'POST',
success: function (data){
console.log('success');
}
});
})
});
$('#btn-2').on('click', function () {
var dummy = $('<input type="file" id="hidden-file" accept=".csv"/>');
dummy.appendTo('body');
dummy.on('change', function (e) {
var file = dummy[0].files[0];
dumpMeta(file);
var reader = new FileReader();
reader.onload = function (e) {
$('#result').text(reader.result);
$(dummy).remove();
};
reader.readAsText(file); // for img: reader.readAsDataURL(file);
});
dummy.click();
$(dummy).prop('disabled', true);
});
</script>
</body>
</html>