Skip to content

Instantly share code, notes, and snippets.

@gengns
Last active April 22, 2018 19:53
Show Gist options
  • Save gengns/74b96ad5b2abdf98d53a00f2d07bc6c5 to your computer and use it in GitHub Desktop.
Save gengns/74b96ad5b2abdf98d53a00f2d07bc6c5 to your computer and use it in GitHub Desktop.
Get local/external raw files without user interface (input file)
/**
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()
}
@gengns
Copy link
Author

gengns commented Feb 23, 2018

It would be nicer using Fetch:

fetch(path)
  .then(response => response.blob())
  .then(data => success_cb(data))
  .catch(error => failure_cb(error))

However, Fetch API do not support URL scheme 'file' :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment