-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
231 lines (197 loc) · 6.21 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Cross-browser File Download</title>
<style>
section {
margin-top: 1em;
}
.support {
display: inline-block;
height: 1em;
width: 1em;
}
.support__success {
background-color: green;
}
.support__fail, #error {
background-color: red;
}
</style>
</head>
<body>
<section>
<p id="error"></p>
</section>
<script>
window.addEventListener('error', function (error) {
document.getElementById('error').textContent = (error.lineno || error.lineNumber) + ' ' + error.message
})
</script>
<h1>Cross-browser File Download</h1>
<form class="download">
<label>lyreco.3.9MB.pdf
<input type="radio" name="fileName" class="download--filename" value="lyreco.3.9MB.pdf" checked>
</label>
<label>example.48K.pdf
<input type="radio" name="fileName" class="download--filename" value="example.48K.pdf">
</label>
<hr>
<input type="submit" class="download--button" value="Save PDF">
<a href="itms-bookss://dotnetcarpenter.github.io/FileReader_Chrome/example.48K.pdf">iBooks</a>
<a href="itms-bookss://example.48K.pdf">iBooks</a>
</form>
<section>
Your browser support:
<ol>
<li>navigator.msSaveBlob: <span class="support" data-test-name="msSaveBlob"></span></li>
<li>URL.createObjectURL: <span class="support" data-test-name="createObjectURL"></span></li>
<li>FileReader::readAsDataURL: <span class="support" data-test-name="readAsDataURL"></span></li>
<li>a:download: <span class="support" data-test-name="download"></span></li>
</ol>
</section>
<script>
Array.prototype.forEach.call(
document.querySelectorAll('.support'),
function (element) {
test(element, element.dataset.testName)
})
function test (element, testName) {
var success = false
switch (testName) {
case 'createObjectURL':
var userAgent = navigator.userAgent
success = URL && URL.createObjectURL
&& userAgent.indexOf('Chrom') !== -1 && userAgent.indexOf('Safari') !== -1
break
case 'readAsDataURL':
var reader = new FileReader
success = reader.readAsDataURL
break
case 'msSaveBlob':
success = navigator.msSaveBlob
break
case 'download':
var a = document.createElement("a")
a.download = 'foo'
success = a.hasAttribute('download')
break
}
if (success) element.classList.add('support__success')
else element.classList.add('support__fail')
}
</script>
<script src="https://unpkg.com/promise-polyfill"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
'use strict'
var form = document.querySelector('.download')
var submitHandler = partial(handler, downloadFile, form)
var getBlob = partial(dot, 'data')
form.addEventListener('submit', submitHandler)
function handler (f, form, event) {
event.preventDefault()
f(form.querySelector('.download--filename:checked').value)
}
function downloadFile (filePath) {
axios.get(filePath, { responseType: 'blob' })
.then(getBlob)
.then(function (blob) {
var fileName = basename(filePath)
var msSaveBlobOr = partial(
iif,
function () { return navigator.msSaveBlob },
function (blob) { navigator.msSaveBlob(blob, fileName) }) // IE11 and Edge 12-18
var saveObjectUrlOrDataUrl = iif(
function () {
var userAgent = navigator.userAgent
return URL && URL.createObjectURL
&& userAgent.indexOf('Chrom') !== -1 && userAgent.indexOf('Safari') !== -1
},
function (readerBlob) { // Chrome and Firefox
var blob = readerBlob.blob
var url = URL.createObjectURL(blob)
saveFile(url, fileName)
URL.revokeObjectURL(url)
},
function (readerBlob) { // Safari
var reader = readerBlob.reader
var url = reader.result
saveFile(url, fileName)
}
)
var save = msSaveBlobOr(function (blob) {
var reader = new FileReader
reader.onloadend = saveObjectUrlOrDataUrl({
reader: reader,
blob: blob,
fileName: fileName
})
reader.readAsDataURL(blob)
})
save(blob)
})
}
function saveFile (file, fileName) {
console.log(file)
var a = document.createElement("a")
a.href = file
a.style.display = 'none'
a.setAttribute('download', 'fileName')
a.download = fileName
document.body.appendChild(a)
a.click()
a.parentNode.removeChild(a)
}
function partial (f) {
var xs = Array.prototype.slice.call(arguments, 1)
var x = xs.shift()
return x == null
? f
: partial.apply(null, [f.bind(null, x)].concat(xs))
}
function dot (property, object) {
return object[property]
}
function iif (expression, truePart, falsePart) {
return function (argument) {
return expression (argument) ? truePart (argument) : falsePart (argument)
}
}
function basename (path) {
var match = path.match(/\/?([^\/]+)$/)
return match ? match[1] : ''
}
/* #region old
if (navigator.msSaveBlob) { // IE11 and Edge 17-
navigator.msSaveBlob(blob, fileName)
} else { // every other browser
var reader = new FileReader()
reader.onloadend = () => {
var url
switch (type) {
case 'createObjectURL':
url = window.URL.createObjectURL(blob)
break
case 'readAsDataURL':
default:
url = reader.result;
}
download(url, fileName)
if (type === 'createObjectURL') {
window.URL.revokeObjectURL(url)
}
}
if (type === 'readAsText') {
reader.readAsText(blob)
} else {
reader.readAsDataURL(blob)
}
}
#endregion */
</script>
</body>
</html>