Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to HTML5 Filesystem API

Introduction to HTML5 Filesystem API

This is my deck for my "Introduction to HTML5 Filesystem API" talk on HannoverJS.

Pascal Precht

July 22, 2012
Tweet

More Decks by Pascal Precht

Other Decks in Programming

Transcript

  1. • Persistent Uploader Local Sandbox, uploading a chunk at a

    time • Video Game, Music, Apps with media assets Using assets from local cache Use cases?
  2. • Persistent Uploader Local Sandbox, uploading a chunk at a

    time • Video Game, Music, Apps with media assets Using assets from local cache • Offline web mail client Storing attachments locally Use cases?
  3. 3 Storage Types • Temporary Storage No prompt, reasonable amount

    of storage is granted by default • Persistent Storage Browser prompt, zero quota granted by default, more storage can be requested
  4. 3 Storage Types • Temporary Storage No prompt, reasonable amount

    of storage is granted by default • Persistent Storage Browser prompt, zero quota granted by default, more storage can be requested • Unlimited Storage Chrome Apps and Extensions, no prompt
  5. window.webkitRequestFileSystem(type, size, onSuccess, onError); webkitStorageInfo.requestQuota(PERSISTENT, bytes, function (bytes) { webkitRequestFileSystem(PERSISTENT,

    bytes, function (fs) { console.log(“Opened filesystem: “+fs.name”); }, function (e) { console.log(“Error: “+ e); }); }, onError);
  6. FileEntry.isFile FileEntry.isDirectory FileEntry.fullPath FileEntry.filesystem FileEntry.getMetadata(onSuccess, onError); FileEntry.moveTo(parentDirEntry, newName, onSuccess, onError);

    FileEntry.copyTo(parentDirEntry, newName, onSuccess, onError); FileEntry.toUrl(); FileEntry.remove(onSuccess, onError); FileEntry.getParent(onSuccess, onError); FileEntry.createWriter(onSuccess, onError); FileEntry.file(onSuccess, onError); The FileEntry
  7. FileEntry.isFile FileEntry.isDirectory FileEntry.fullPath FileEntry.filesystem // filesystem on which the entry

    resides FileEntry.getMetadata(onSuccess, onError); FileEntry.moveTo(parentDirEntry, newName, onSuccess, onError); FileEntry.copyTo(parentDirEntry, newName, onSuccess, onError); FileEntry.toUrl(); FileEntry.remove(onSuccess, onError); FileEntry.getParent(onSuccess, onError); FileEntry.createWriter(onSuccess, onError); FileEntry.file(onSuccess, onError); The FileEntry
  8. FileEntry.isFile FileEntry.isDirectory FileEntry.fullPath FileEntry.filesystem FileEntry.getMetadata(onSuccess, onError); FileEntry.moveTo(parentDirEntry, newName, onSuccess, onError);

    FileEntry.copyTo(parentDirEntry, newName, onSuccess, onError); FileEntry.toUrl(); // Returns a filesystem: URL FileEntry.remove(onSuccess, onError); FileEntry.getParent(onSuccess, onError); FileEntry.createWriter(onSuccess, onError); FileEntry.file(onSuccess, onError); The FileEntry
  9. FileEntry.isFile FileEntry.isDirectory FileEntry.fullPath FileEntry.filesystem FileEntry.getMetadata(onSuccess, onError); FileEntry.moveTo(parentDirEntry, newName, onSuccess, onError);

    FileEntry.copyTo(parentDirEntry, newName, onSuccess, onError); FileEntry.toUrl(); FileEntry.remove(onSuccess, onError); FileEntry.getParent(onSuccess, onError); FileEntry.createWriter(onSuccess, onError); // new FileWriter FileEntry.file(onSuccess, onError); The FileEntry
  10. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {create: true}, function (fileEntry)

    { fileEntry.getMetadata(function (md) { console.log(md.modificationTime.toDateString()); }); }); }); Creating a File
  11. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {create: true}, function (fileEntry)

    { fileEntry.getMetadata(function (md) { console.log(md.modificationTime.toDateString()); }); }); }); Creating a File
  12. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {create: true}, function (fileEntry)

    { fileEntry.getMetadata(function (md) { console.log(md.modificationTime.toDateString()); }); }); }); Creating a File
  13. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {create: true}, function (fileEntry)

    { fileEntry.getMetadata(function (md) { console.log(md.modificationTime.toDateString()); }); }); }); Creating a File
  14. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {create: true}, function (fileEntry)

    { fileEntry.getMetadata(function (md) { console.log(md.modificationTime.toDateString()); }); }); }); Creating a File
  15. FileReader.error FileReader.readyState FileReader.result // file’s contents FileReader.abort(blob); FileReader.readAsArrayBuffer(blob); FileReader.readAsBinaryString(blob); FileReader.readAsDataUrl(blob);

    FileReader.readAsText(file); FileReader.onabort FileReader.onload FileReader.onloadstart FileReader.onloadend FileReader.onprogress The FileReader
  16. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {}, function (fileEntry) {

    fileEntry.file(function (file) { var reader = new FileReader(); reader.onloadend = function (e) { var textarea = document.createElement(‘textarea’); textarea.value = this.result; document.body.appendChild(textarea); }; reader.readAsText(file); }); }); }); Reading a File
  17. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {}, function (fileEntry) {

    fileEntry.file(function (file) { var reader = new FileReader(); reader.onloadend = function (e) { var textarea = document.createElement(‘textarea’); textarea.value = this.result; document.body.appendChild(textarea); }; reader.readAsText(file); }); }); }); Reading a File
  18. FileReader.position FileReader.length FileReader.error FileReader.readyState // One of 3 states: INIT,

    WRITING, DONE FileReader.abort(); FileReader.write(blob); FileReader.seek(offset); FileReader.truncate(size); FileReader.onabort FileReader.onerror FileReader.onprogress FileReader.onwrite FileReader.onwritestart FileReader.onwriteend The FileWriter
  19. FileReader.position FileReader.length FileReader.error FileReader.readyState FileReader.abort(); FileReader.write(blob); FileReader.seek(offset); FileReader.truncate(size); // Changes

    the length of a file FileReader.onabort FileReader.onerror FileReader.onprogress FileReader.onwrite FileReader.onwritestart FileReader.onwriteend The FileWriter
  20. FileReader.position FileReader.length FileReader.error FileReader.readyState FileReader.abort(); FileReader.write(blob); FileReader.seek(offset); FileReader.truncate(size); FileReader.onabort FileReader.onerror

    FileReader.onprogress FileReader.onwrite // write operation successfully completed FileReader.onwritestart FileReader.onwriteend The FileWriter
  21. FileReader.position FileReader.length FileReader.error FileReader.readyState FileReader.abort(); FileReader.write(blob); FileReader.seek(offset); FileReader.truncate(size); FileReader.onabort FileReader.onerror

    FileReader.onprogress FileReader.onwrite FileReader.onwritestart // before writing is about to start FileReader.onwriteend The FileWriter
  22. FileReader.position FileReader.length FileReader.error FileReader.readyState FileReader.abort(); FileReader.write(blob); FileReader.seek(offset); FileReader.truncate(size); FileReader.onabort FileReader.onerror

    FileReader.onprogress FileReader.onwrite FileReader.onwritestart FileReader.onwriteend // write complete, whether successful or not The FileWriter
  23. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {}, function (fileEntry) {

    fileEntry.createWriter(function (fileWriter) { var blob = new Blob( [“Hello World”], {“type”:”text/plain”}); fileWriter.write(blob); }); }); }); Writing a File
  24. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {}, function (fileEntry) {

    fileEntry.createWriter(function (fileWriter) { var blob = new Blob( [“Hello World”], {“type”:”text/plain”}); fileWriter.write(blob); }); }); }); Writing a File
  25. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {}, function (fileEntry) {

    fileEntry.createWriter(function (fileWriter) { var toAppend = new Blob([“Appended sentence”], { “type”:”text/plain” }); fileWriter.seek(fileWriter.length); fileWriter.write(toAppend); }); }); }); Appending Data
  26. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {}, function (fileEntry) {

    fileEntry.createWriter(function (fileWriter) { var toAppend = new Blob([“Appended sentence”], { “type”:”text/plain” }); fileWriter.seek(fileWriter.length); fileWriter.write(toAppend); }); }); }); Appending Data
  27. webkitRequestFileSystem(PERSISTENT, bytes, function (fs) { fs.root.getFile('example.txt', {}, function (fileEntry) {

    fileEntry.createWriter(function (fileWriter) { var toAppend = new Blob([“Appended sentence”], { “type”:”text/plain” }); fileWriter.seek(fileWriter.length); fileWriter.write(toAppend); }); }); }); Appending Data
  28. • Use <input type=”file”> User selects files from hard drive

    • HTML5 Drag And Drop Nearly the same. (Checkout Dnd.js!) Importing Files
  29. • Use <input type=”file”> User selects files from hard drive

    • HTML5 Drag And Drop Nearly the same. (Checkout Dnd.js!) • XHR2 Fetching remote binary data Importing Files
  30. • Use <input type=”file”> User selects files from hard drive

    • HTML5 Drag And Drop Nearly the same. (Checkout Dnd.js!) • XHR2 Fetching remote binary data • Copy and Paste Events Apps can read clipboard information Importing Files
  31. document.querySelector(“input[type=file]”).onchange = function (e) { var files = this.files; webkitRequestFileSystem(TEMPORARY,

    1024*1024, function (fs) { var i = 0, file; for(; file = files[i]; i++) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function(fileWriter) { fileWriter.write(file); }); }); } }); });
  32. document.querySelector(“input[type=file]”).onchange = function (e) { var files = this.files; webkitRequestFileSystem(TEMPORARY,

    1024*1024, function (fs) { var i = 0, file; for(; file = files[i]; i++) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function(fileWriter) { fileWriter.write(file); }); }); } }); });
  33. document.querySelector(“input[type=file]”).onchange = function (e) { var files = this.files; webkitRequestFileSystem(TEMPORARY,

    1024*1024, function (fs) { var i = 0, file; for(; file = files[i]; i++) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function(fileWriter) { fileWriter.write(file); }); }); } }); });
  34. document.querySelector(“input[type=file]”).onchange = function (e) { var files = this.files; webkitRequestFileSystem(TEMPORARY,

    1024*1024, function (fs) { var i = 0, file; for(; file = files[i]; i++) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function(fileWriter) { fileWriter.write(file); }); }); } }); });
  35. document.querySelector(“input[type=file]”).onchange = function (e) { var files = this.files; webkitRequestFileSystem(TEMPORARY,

    1024*1024, function (fs) { var i = 0, file; for(; file = files[i]; i++) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function(fileWriter) { fileWriter.write(file); }); }); } }); });
  36. document.querySelector(“input[type=file]”).onchange = function (e) { var files = this.files; webkitRequestFileSystem(TEMPORARY,

    1024*1024, function (fs) { var i = 0, file; for(; file = files[i]; i++) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function(fileWriter) { fileWriter.write(file); }); }); } }); });
  37. dnd.on(‘#dropArea’, { ‘enter’: function (element) { element.classList.add(‘dropping’); }, ‘leave’: function

    (element) { element.classList.remove(‘dropping’); }, ‘drop’: function (element, files) { [].forEach.call(files, function (file, i) { webkitRequestFileSystem(TEMPORARY, 1024*1024, function (fs) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function (writer) { writer.write(file); }); }); }); } });
  38. dnd.on(‘#dropArea’, { ‘enter’: function (element) { element.classList.add(‘dropping’); }, ‘leave’: function

    (element) { element.classList.remove(‘dropping’); }, ‘drop’: function (element, files) { [].forEach.call(files, function (file, i) { webkitRequestFileSystem(TEMPORARY, 1024*1024, function (fs) { fs.root.getFile(file.name, {create: true}, function (fileEntry) { fileEntry.createWriter(function (writer) { writer.write(file); }); }); }); } });
  39. INTRODUCTION TO HTML FILESYSTEM API Pascal Precht Twitter: @PascalPrecht Blog:

    hopelessart.com Slides: speakerdeck.com/u/pascalprecht/p/introduction-to-html5- filesystem-api