Large file upload with WebSocket

Use web workers for large files processing instead doing it in main thread and upload chunks of file data using file.slice(). This article helps you to handle large files in workers. change XHR send to Websocket in main thread. //Messages from worker function onmessage(blobOrFile) { ws.send(blobOrFile); } //construct file on server side based on blob … Read more

Writing file to desktop using HTML5 FileSystem API

Please note that Filesystem API is no longer part of the standard’s specification, as specified at: http://www.w3.org/TR/file-system-api/ EDIT: Quoting the specification in case the link changes: “File API: Directories and System W3C Working Group Note 24 April 2014 Work on this document has been discontinued and it should not be referenced or used as a … Read more

Convert base64 png data to javascript file objects

Way 1: only works for dataURL, not for other types of url. function dataURLtoFile(dataurl, filename) { var arr = dataurl.split(‘,’), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n–){ u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], filename, {type:mime}); } //Usage example: var file = dataURLtoFile(‘data:image/png;base64,……’, ‘a.png’); console.log(file); Way 2: works … Read more

restrict file upload selection to specific types

There is an html attribute for this specific purpose called accept but it has little support across browsers. Because of this server side validation is recommended instead. <input type=”file” name=”pic” id=”pic” accept=”image/gif, image/jpeg” /> If you don’t have access to the backend have a look at a flash based solution like SWFUpload. See more on … Read more

Adding UTF-8 BOM to string/Blob

Prepend \ufeff to the string. See http://msdn.microsoft.com/en-us/library/ie/2yfce773(v=vs.94).aspx See discussion between @jeff-fischer and @casey for details on UTF-8 and UTF-16 and the BOM. What actually makes the above work is that the string \ufeff is always used to represent the BOM, regardless of UTF-8 or UTF-16 being used. See p.36 in The Unicode Standard 5.0, Chapter … Read more

How FileReader.readAsText in HTML5 File API works?

FileReader load event sets the .result value asynchronously. To access the .result use load or loadend event. When a file has been selected at <input type=”file”> Choose File or Browse… UI, deleting file at local filesystem should not effect the File object at FileList returned by .files call. See 2.9.2. Transferable objects, 6.7.3 The DataTransfer … Read more