Last active
April 22, 2018 19:53
-
-
Save gengns/74b96ad5b2abdf98d53a00f2d07bc6c5 to your computer and use it in GitHub Desktop.
Get local/external raw files without user interface (input file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Get local/external files without user interface (input file) | |
@param path ex: | |
path = 'file:///storage/emulated/0/Android/data/com.gengns.appname/cache/finn.jpg' | |
path = 'https://allmyimages.com/jake.jpg' | |
@param success | |
@param failure | |
*/ | |
function getFile(path, success, failure) { | |
const xhr = new XMLHttpRequest() | |
xhr.open('GET', path) | |
xhr.responseType = 'blob' // raw data | |
xhr.onload = () => { | |
if (xhr.status == 0 || (xhr.status >= 200 && xhr.status < 400) && success) | |
success(xhr.response) | |
else if (failure) | |
failure(xhr.status) | |
} | |
xhr.send() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be nicer using Fetch:
However, Fetch API do not support URL scheme 'file' :(