gengns ES

An entry from the gengns log, written by Génesis García Morilla · · JavaScript

Get local/external raw files without user interface

get_file.js

/**
  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()
}
View on GitHub Gist

Tags: AJAX, binary, blob, file