Normally just doing FileReader.read...
and catapulting on errors would be the (async.) way.
But in some cases simply checking access synchronously before initiating the actual transfer may be better (see examples below).
- Can it be something like this (in pseudocode; based on this SO post)?
Can it return true without actually reading the whole file?
function is_readable(file) { // File object var ok = null function check(e) { ok = (SOME.ERROR.CONDITION.HERE ? false : true) // perhaps: e.target.result? } var blob = file.slice(0, 1) // does slice() make any sense here? var reader = new FileReader() // not sure which events to buy: reader.onload = check reader.onerror = check reader.readAsArrayBuffer(blob) // sync here for the result: while (ok === null) { alert('...waiting...') } return ok }
Example cases:
For batch upload (selecting files with a multi-file
INPUT
, and not sending them until an "Upload All" button is pressed), you want to list unreadable files in red. For this, in the list builder loop, you'd just select color for eachFile
objectf
likeis_readable(f) ? 'green' : 'red'
.Chrome(25) doesn't seem to set an error during
XMLHttpRequest.send()
here for some reason, happily posting the file, but silently truncating to 0 bytes, if the file is not readable. So I'd like to check it myself beforehand, as a workaround.
(Note: I checked php.js but it doesn't seem to have this one.)