Skip to content

Instantly share code, notes, and snippets.

@jfoclpf
Last active January 9, 2021 14:02
Show Gist options
  • Save jfoclpf/07e52f6bdf9c967449c4bc06af44c94a to your computer and use it in GitHub Desktop.
Save jfoclpf/07e52f6bdf9c967449c4bc06af44c94a to your computer and use it in GitHub Desktop.

Revisions

  1. jfoclpf revised this gist Jan 9, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,7 @@ function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    }
    ```

    An exemple for download
    An example for download

    ```js
    downloadFileToDevice('https://example.com/img.jpg',
  2. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -79,7 +79,7 @@ An exemple for download
    ```js
    downloadFileToDevice('https://example.com/img.jpg',
    'myImg.jpg',
    cordova.file.externalCacheDirectory,
    cordova.file.cacheDirectory,
    (err, localFilePath) => {
    if (err) {
    console.error('An error occured downloading file:', err)
  3. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    The cordova plugin `cordova-plugin-file-transfer` is [due to be deprecated](https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html) and its latest [npm version fails on iOS](https://github.com/apache/cordova-plugin-file-transfer/issues/297).

    Therefore these two working functions are purely based on JS, and thus no need to use extra plugins, besides the standard plugin `cordova-plugin-file`
    Therefore these two working functions are purely based on JS, and thus no need to use extra plugins, besides the standard plugin `cordova-plugin-file`. Therefore this is compatible with any platform.

    ```js
    // for different types of cordovaFileSystem check here:
  4. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## Pure Javascript download and upload functions for Apache Cordova
    ## Pure Vanilla Javascript download and upload functions for Apache Cordova

    The cordova plugin `cordova-plugin-file-transfer` is [due to be deprecated](https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html) and its latest [npm version fails on iOS](https://github.com/apache/cordova-plugin-file-transfer/issues/297).

  5. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    var xhr = new XMLHttpRequest()
    xhr.open('POST', remoteUrl, true)
    xhr.onload = function () {
    if (xhr.status === 200) {
    if (xhr.status === 200 || xhr.status === 201) {
    if (typeof callback === 'function') { callback() }
    } else {
    console.error('Error on xhr.status: ' + xhr.status); onerror(xhr.status)
  6. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,6 @@ function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    xhr.open('POST', remoteUrl, true)
    xhr.onload = function () {
    if (xhr.status === 200) {
    console.success(`File ${fileUri} uploaded succesfully to url ${remoteUrl}`)
    if (typeof callback === 'function') { callback() }
    } else {
    console.error('Error on xhr.status: ' + xhr.status); onerror(xhr.status)
  7. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 17 additions and 11 deletions.
    28 changes: 17 additions & 11 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -39,33 +39,39 @@ function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {

    ```js
    function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    var onerror = (err) => {
    console.error(`Error uploading file ${fileUri} to ${remoteUrl}`,
    err, new Error(err))
    if (typeof callback === 'function') { callback(Error(err)) }
    }

    window.resolveLocalFileSystemURL(fileUri, function (fileEntry) {
    fileEntry.file(function (file) {
    fileEntry.file((file) => {
    if (!file.size) { onerror('File is empty (on fileEntry from resolveLocalFileSystemURL)'); return }
    var reader = new FileReader()
    reader.onloadend = function () {
    var blob = new Blob([new Uint8Array(this.result)], { type: 'application/octet-stream' })
    reader.onloadend = () => {
    var blob = new Blob([new Uint8Array(reader.result)], { type: 'application/octet-stream' })
    if (!blob.size) { onerror('File is empty (on blob)'); return }
    var fd = new FormData()
    fd.append('file', blob, fileName)

    var xhr = new XMLHttpRequest()
    xhr.open('POST', remoteUrl, true)
    xhr.onload = function () {
    if (xhr.status === 200) {
    console.success(`File ${fileUri} uploaded succesfully to url ${remoteUrl}`)
    if (typeof callback === 'function') { callback() }
    } else {
    if (typeof callback === 'function') { callback(xhr.status) }
    console.error('Error on xhr.status: ' + xhr.status); onerror(xhr.status)
    }
    }
    xhr.onerror = function (err) {
    if (typeof callback === 'function') { callback(err) }
    }
    xhr.onerror = (err) => { console.error('Error on XMLHttpRequest'); onerror(err) }
    xhr.send(fd)
    }
    reader.onerror = (err) => { console.error('Error on FileReader'); onerror(err) }
    reader.readAsArrayBuffer(file)
    }, function (err) {
    if (typeof callback === 'function') { callback(err) }
    })
    })
    }, (err) => { console.error('Error on fileEntry.file'); onerror(err) })
    }, (err) => { console.error('Error on resolveLocalFileSystemURL'); onerror(err) })
    }
    ```

  8. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    blob = xhr.response // xhr.response is now a blob object
    var DataBlob = blob
    window.resolveLocalFileSystemURL(cordovaFileSystem, (dirEntry) => {
    const sanitizedFilename = filename.replace(/[^a-z0-9]/gi, '_').toLowerCase() // sanitize filename
    const sanitizedFilename = filename.replace(/[^a-z0-9\.]/gi, '_').toLowerCase() // sanitize filename
    dirEntry.getFile(sanitizedFilename, { create: true }, (file) => {
    file.createWriter((fileWriter) => {
    fileWriter.write(DataBlob)
  9. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ Therefore these two working functions are purely based on JS, and thus no need t
    ```js
    // for different types of cordovaFileSystem check here:
    // https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#where-to-store-files
    // or simple type in the console `console.log(cordova.file)`
    // or simply type in the console `console.log(cordova.file)`
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var onerror = (err) => {
    console.error(`Error downloading from ${fileurl} to cordovaFileSystem ${cordovaFileSystem}`,
  10. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ Therefore these two working functions are purely based on JS, and thus no need t
    // or simple type in the console `console.log(cordova.file)`
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var onerror = (err) => {
    console.error(`Error downloading from ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`,
    console.error(`Error downloading from ${fileurl} to cordovaFileSystem ${cordovaFileSystem}`,
    err, new Error(err))
    if (typeof callback === 'function') { callback(Error(err)) }
    }
  11. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ Therefore these two working functions are purely based on JS, and thus no need t
    // or simple type in the console `console.log(cordova.file)`
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var onerror = (err) => {
    console.error(`Error downloading file from url ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`,
    console.error(`Error downloading from ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`,
    err, new Error(err))
    if (typeof callback === 'function') { callback(Error(err)) }
    }
  12. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,7 @@ Therefore these two working functions are purely based on JS, and thus no need t
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var onerror = (err) => {
    console.error(`Error downloading file from url ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`,
    err,
    new Error(err))
    err, new Error(err))
    if (typeof callback === 'function') { callback(Error(err)) }
    }

  13. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,9 @@ The cordova plugin `cordova-plugin-file-transfer` is [due to be deprecated](http
    Therefore these two working functions are purely based on JS, and thus no need to use extra plugins, besides the standard plugin `cordova-plugin-file`

    ```js
    // download file to device
    // for different types of cordovaFileSystem check here: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#where-to-store-files
    // or simple in the console type `console.log(cordova.file)`
    // for different types of cordovaFileSystem check here:
    // https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#where-to-store-files
    // or simple type in the console `console.log(cordova.file)`
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var onerror = (err) => {
    console.error(`Error downloading file from url ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`,
  14. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,9 @@ Therefore these two working functions are purely based on JS, and thus no need t
    // or simple in the console type `console.log(cordova.file)`
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var onerror = (err) => {
    console.error(`Error downloading file from url ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`, err, new Error(err))
    console.error(`Error downloading file from url ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`,
    err,
    new Error(err))
    if (typeof callback === 'function') { callback(Error(err)) }
    }

  15. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,6 @@ function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    dirEntry.getFile(sanitizedFilename, { create: true }, (file) => {
    file.createWriter((fileWriter) => {
    fileWriter.write(DataBlob)
    console.success(`File downloaded succesfully from url ${fileurl} to ${cordovaFileSystem + sanitizedFilename}`)
    if (typeof callback === 'function') { callback(null, cordovaFileSystem + sanitizedFilename) }
    }, (err) => { console.error('Error on file.createWriter'); onerror(err) })
    }, (err) => { console.error('Error on dirEntry.getFile'); onerror(err) })
  16. jfoclpf revised this gist Jan 8, 2021. 1 changed file with 18 additions and 10 deletions.
    28 changes: 18 additions & 10 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -9,27 +9,35 @@ Therefore these two working functions are purely based on JS, and thus no need t
    // for different types of cordovaFileSystem check here: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#where-to-store-files
    // or simple in the console type `console.log(cordova.file)`
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var onerror = (err) => {
    console.error(`Error downloading file from url ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`, err, new Error(err))
    if (typeof callback === 'function') { callback(Error(err)) }
    }

    var blob = null
    var xhr = new XMLHttpRequest()
    xhr.open('GET', fileurl)
    xhr.responseType = 'blob' // force the HTTP response, response-type header to be blob
    xhr.onload = function () {
    xhr.onload = () => {
    blob = xhr.response // xhr.response is now a blob object
    var DataBlob = blob
    window.resolveLocalFileSystemURL(cordovaFileSystem, function (dir) {
    dir.getFile(filename, { create: true }, function (file) {
    file.createWriter(function (fileWriter) {
    window.resolveLocalFileSystemURL(cordovaFileSystem, (dirEntry) => {
    const sanitizedFilename = filename.replace(/[^a-z0-9]/gi, '_').toLowerCase() // sanitize filename
    dirEntry.getFile(sanitizedFilename, { create: true }, (file) => {
    file.createWriter((fileWriter) => {
    fileWriter.write(DataBlob)
    if (typeof callback === 'function') { callback(null, cordovaFileSystem + filename) }
    }, function (err) {
    if (typeof callback === 'function') { callback(err) }
    })
    })
    })
    console.success(`File downloaded succesfully from url ${fileurl} to ${cordovaFileSystem + sanitizedFilename}`)
    if (typeof callback === 'function') { callback(null, cordovaFileSystem + sanitizedFilename) }
    }, (err) => { console.error('Error on file.createWriter'); onerror(err) })
    }, (err) => { console.error('Error on dirEntry.getFile'); onerror(err) })
    }, (err) => { console.error('Error on resolveLocalFileSystemURL'); onerror(err) })
    }
    xhr.onerror = (err) => { console.error('Error on XMLHttpRequest'); onerror(err) }
    xhr.send()
    }
    ```

    ```js
    function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    window.resolveLocalFileSystemURL(fileUri, function (fileEntry) {
    fileEntry.file(function (file) {
  17. jfoclpf revised this gist Jan 6, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@ downloadFileToDevice('https://example.com/img.jpg',
    })
    ```

    An example on how to call it, exactly as the `ft.upload` in `cordova-plugin-file-transfer`
    An example for upload, exactly as the `ft.upload` in `cordova-plugin-file-transfer`

    ```js
    uploadFileToServer('file:///storage/emulated/0/Android/data/myfile.jpg',
  18. jfoclpf revised this gist Jan 6, 2021. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -61,6 +61,21 @@ function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    }
    ```

    An exemple for download

    ```js
    downloadFileToDevice('https://example.com/img.jpg',
    'myImg.jpg',
    cordova.file.externalCacheDirectory,
    (err, localFilePath) => {
    if (err) {
    console.error('An error occured downloading file:', err)
    } else {
    console.log('Download file with success: ' + localFilePath)
    }
    })
    ```

    An example on how to call it, exactly as the `ft.upload` in `cordova-plugin-file-transfer`

    ```js
  19. jfoclpf revised this gist Jan 6, 2021. 1 changed file with 1 addition and 11 deletions.
    12 changes: 1 addition & 11 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -20,11 +20,8 @@ function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    dir.getFile(filename, { create: true }, function (file) {
    file.createWriter(function (fileWriter) {
    fileWriter.write(DataBlob)
    console.log(`File downloaded succesfully from url ${fileurl} to ${cordovaFileSystem + filename}`)
    if (typeof callback === 'function') { callback(null, cordovaFileSystem + filename) }
    }, function (err) {
    console.error(`Error downloading file from url: ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`)
    console.error(err)
    if (typeof callback === 'function') { callback(err) }
    })
    })
    @@ -40,31 +37,24 @@ function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    reader.onloadend = function () {
    var blob = new Blob([new Uint8Array(this.result)], { type: 'application/octet-stream' })
    var fd = new FormData()

    fd.append('file', blob, fileName)

    var xhr = new XMLHttpRequest()
    xhr.open('POST', remoteUrl, true)
    xhr.onload = function () {
    if (xhr.status === 200) {
    console.log(`File ${fileUri} uploaded succesfully to url ${remoteUrl}`)
    if (typeof callback === 'function') { callback() }
    } else {
    console.error(`Error uploading file ${fileUri}. Server returned ${xhr.status}`)
    if (typeof callback === 'function') { callback(xhr.status) }
    }
    }
    xhr.onerror = function (err) {
    console.error(`Error uploading file ${fileUri} to server`)
    console.error(err)
    if (typeof callback === 'function') { callback(err) }
    }
    xhr.send(fd)
    }
    reader.readAsArrayBuffer(file)
    }, function (err) {
    console.error(`Error uploading file ${fileUri} to server`)
    console.error(err)
    if (typeof callback === 'function') { callback(err) }
    })
    })
    @@ -79,7 +69,7 @@ uploadFileToServer('file:///storage/emulated/0/Android/data/myfile.jpg',
    'https://example.com/upload_url',
    (err) => {
    if (err) {
    console.error('Error uploading file', err)
    console.error('Error uploading file:', err)
    } else {
    console.log('Upload done it with success')
    }
  20. jfoclpf revised this gist Jan 5, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -75,7 +75,7 @@ An example on how to call it, exactly as the `ft.upload` in `cordova-plugin-file

    ```js
    uploadFileToServer('file:///storage/emulated/0/Android/data/myfile.jpg',
    'myfile.jpg',
    'myfileNameOnServer.jpg',
    'https://example.com/upload_url',
    (err) => {
    if (err) {
  21. jfoclpf revised this gist Jan 5, 2021. 1 changed file with 19 additions and 2 deletions.
    21 changes: 19 additions & 2 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    The cordova plugin `cordova-plugin-file-transfer` is due to be deprecated and its latest [npm version fails on iOS](https://github.com/apache/cordova-plugin-file-transfer/issues/297).
    ## Pure Javascript download and upload functions for Apache Cordova

    The cordova plugin `cordova-plugin-file-transfer` is [due to be deprecated](https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html) and its latest [npm version fails on iOS](https://github.com/apache/cordova-plugin-file-transfer/issues/297).

    Therefore these two working functions are purely based on JS, and thus no need to use extra plugins, besides the standard plugin `cordova-plugin-file`

    @@ -67,4 +69,19 @@ function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    })
    })
    }
    ```
    ```

    An example on how to call it, exactly as the `ft.upload` in `cordova-plugin-file-transfer`

    ```js
    uploadFileToServer('file:///storage/emulated/0/Android/data/myfile.jpg',
    'myfile.jpg',
    'https://example.com/upload_url',
    (err) => {
    if (err) {
    console.error('Error uploading file', err)
    } else {
    console.log('Upload done it with success')
    }
    })
    ```
  22. jfoclpf created this gist Jan 5, 2021.
    70 changes: 70 additions & 0 deletions pureJSUploadDownloadForCordova.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    The cordova plugin `cordova-plugin-file-transfer` is due to be deprecated and its latest [npm version fails on iOS](https://github.com/apache/cordova-plugin-file-transfer/issues/297).

    Therefore these two working functions are purely based on JS, and thus no need to use extra plugins, besides the standard plugin `cordova-plugin-file`

    ```js
    // download file to device
    // for different types of cordovaFileSystem check here: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#where-to-store-files
    // or simple in the console type `console.log(cordova.file)`
    function downloadFileToDevice (fileurl, filename, cordovaFileSystem, callback) {
    var blob = null
    var xhr = new XMLHttpRequest()
    xhr.open('GET', fileurl)
    xhr.responseType = 'blob' // force the HTTP response, response-type header to be blob
    xhr.onload = function () {
    blob = xhr.response // xhr.response is now a blob object
    var DataBlob = blob
    window.resolveLocalFileSystemURL(cordovaFileSystem, function (dir) {
    dir.getFile(filename, { create: true }, function (file) {
    file.createWriter(function (fileWriter) {
    fileWriter.write(DataBlob)
    console.log(`File downloaded succesfully from url ${fileurl} to ${cordovaFileSystem + filename}`)
    if (typeof callback === 'function') { callback(null, cordovaFileSystem + filename) }
    }, function (err) {
    console.error(`Error downloading file from url: ${fileurl} to cordovaFileSystem: ${cordovaFileSystem}`)
    console.error(err)
    if (typeof callback === 'function') { callback(err) }
    })
    })
    })
    }
    xhr.send()
    }

    function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
    window.resolveLocalFileSystemURL(fileUri, function (fileEntry) {
    fileEntry.file(function (file) {
    var reader = new FileReader()
    reader.onloadend = function () {
    var blob = new Blob([new Uint8Array(this.result)], { type: 'application/octet-stream' })
    var fd = new FormData()

    fd.append('file', blob, fileName)

    var xhr = new XMLHttpRequest()
    xhr.open('POST', remoteUrl, true)
    xhr.onload = function () {
    if (xhr.status === 200) {
    console.log(`File ${fileUri} uploaded succesfully to url ${remoteUrl}`)
    if (typeof callback === 'function') { callback() }
    } else {
    console.error(`Error uploading file ${fileUri}. Server returned ${xhr.status}`)
    if (typeof callback === 'function') { callback(xhr.status) }
    }
    }
    xhr.onerror = function (err) {
    console.error(`Error uploading file ${fileUri} to server`)
    console.error(err)
    if (typeof callback === 'function') { callback(err) }
    }
    xhr.send(fd)
    }
    reader.readAsArrayBuffer(file)
    }, function (err) {
    console.error(`Error uploading file ${fileUri} to server`)
    console.error(err)
    if (typeof callback === 'function') { callback(err) }
    })
    })
    }
    ```