2 title: "Reading a local file with the File API"
7 <h3>Choose the local(s) zip file(s)</h3>
8 <p class="note">Note : your browser will process the zip file, don't choose a file too big !</p>
9 <input type="file" id="file" name="file" multiple /><br />
11 <div id="error_block" class="alert alert-danger hidden">
12 You will need a recent browser to use this demo :(
15 <div id="result_block" class="hidden">
17 <div id="result"></div>
19 <script type="text/javascript" src="{{site.baseurl}}/test/jquery-1.8.3.min.js"></script>
20 <script type="text/javascript">
22 if (!window.FileReader || !window.ArrayBuffer) {
23 $("#error_block").removeClass("hidden").addClass("show");
28 var $result = $("#result");
29 $("#file").on("change", function(evt) {
32 // be sure to show the results
33 $("#result_block").removeClass("hidden").addClass("show");
35 // see http://www.html5rocks.com/en/tutorials/file/dndfiles/
37 var files = evt.target.files;
38 for (var i = 0, f; f = files[i]; i++) {
40 var reader = new FileReader();
42 // Closure to capture the file information.
43 reader.onload = (function(theFile) {
45 var $title = $("<h4>", {
48 $result.append($title);
49 var $fileContent = $("<ul>");
52 var dateBefore = new Date();
53 // read the content of the file with JSZip
54 var zip = new JSZip(e.target.result);
55 var dateAfter = new Date();
57 $title.append($("<span>", {
58 text:" (parsed in " + (dateAfter - dateBefore) + "ms)"
61 // that, or a good ol' for(var entryName in zip.files)
62 $.each(zip.files, function (index, zipEntry) {
63 $fileContent.append($("<li>", {
66 // the content is here : zipEntry.asText()
71 $fileContent = $("<div>", {
72 "class" : "alert alert-danger",
73 text : "Error reading " + theFile.name + " : " + e.message
76 $result.append($fileContent);
81 // readAsArrayBuffer and readAsBinaryString both produce valid content for JSZip.
82 reader.readAsArrayBuffer(f);
83 // reader.readAsBinaryString(f);