Last active
January 9, 2021 14:02
-
-
Save jfoclpf/07e52f6bdf9c967449c4bc06af44c94a to your computer and use it in GitHub Desktop.
Revisions
-
jfoclpf revised this gist
Jan 9, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -74,7 +74,7 @@ function uploadFileToServer (fileUri, fileName, remoteUrl, callback) { } ``` An example for download ```js downloadFileToDevice('https://example.com/img.jpg', -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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.cacheDirectory, (err, localFilePath) => { if (err) { console.error('An error occured downloading file:', err) -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 this is compatible with any platform. ```js // for different types of cordovaFileSystem check here: -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ ## 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). -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 || xhr.status === 201) { if (typeof callback === 'function') { callback() } } else { console.error('Error on xhr.status: ' + xhr.status); onerror(xhr.status) -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) { if (typeof callback === 'function') { callback() } } else { console.error('Error on xhr.status: ' + xhr.status); onerror(xhr.status) -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 17 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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((file) => { if (!file.size) { onerror('File is empty (on fileEntry from resolveLocalFileSystemURL)'); return } var reader = new FileReader() 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 { console.error('Error on xhr.status: ' + xhr.status); onerror(xhr.status) } } 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) }, (err) => { console.error('Error on fileEntry.file'); onerror(err) }) }, (err) => { console.error('Error on resolveLocalFileSystemURL'); onerror(err) }) } ``` -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 dirEntry.getFile(sanitizedFilename, { create: true }, (file) => { file.createWriter((fileWriter) => { fileWriter.write(DataBlob) -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 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}`, -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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}`, err, new Error(err)) if (typeof callback === 'function') { callback(Error(err)) } } -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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}`, err, new Error(err)) if (typeof callback === 'function') { callback(Error(err)) } } -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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)) if (typeof callback === 'function') { callback(Error(err)) } } -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 // 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}`, -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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)) if (typeof callback === 'function') { callback(Error(err)) } } -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) 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) }) -
jfoclpf revised this gist
Jan 8, 2021 . 1 changed file with 18 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 = () => { 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 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) }) }, (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) { -
jfoclpf revised this gist
Jan 6, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -76,7 +76,7 @@ downloadFileToDevice('https://example.com/img.jpg', }) ``` An example for upload, exactly as the `ft.upload` in `cordova-plugin-file-transfer` ```js uploadFileToServer('file:///storage/emulated/0/Android/data/myfile.jpg', -
jfoclpf revised this gist
Jan 6, 2021 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 -
jfoclpf revised this gist
Jan 6, 2021 . 1 changed file with 1 addition and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) if (typeof callback === 'function') { callback(null, cordovaFileSystem + filename) } }, function (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) { if (typeof callback === 'function') { callback() } } else { if (typeof callback === 'function') { callback(xhr.status) } } } xhr.onerror = function (err) { if (typeof callback === 'function') { callback(err) } } xhr.send(fd) } reader.readAsArrayBuffer(file) }, function (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) } else { console.log('Upload done it with success') } -
jfoclpf revised this gist
Jan 5, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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', 'myfileNameOnServer.jpg', 'https://example.com/upload_url', (err) => { if (err) { -
jfoclpf revised this gist
Jan 5, 2021 . 1 changed file with 19 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,6 @@ ## 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') } }) ``` -
jfoclpf created this gist
Jan 5, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) } }) }) } ```