From 08ec3203e9144d9abea69a430c4916c57749dec6 Mon Sep 17 00:00:00 2001 From: Tobias Becht Date: Tue, 1 Feb 2022 14:56:33 +0100 Subject: [PATCH 1/3] feat: response object on file download --- www/helpers.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/www/helpers.js b/www/helpers.js index 5fefebf..8ab55f0 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -343,7 +343,10 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, function injectFileEntryHandler(cb) { return function (response) { - cb(createFileEntry(response.file)); + var fileEntry = createFileEntry(response.file); + // optional: delete response.file; + response.data = fileEntry; + cb(fileEntry, response); } } From 48445786b639e1720e79403b97f319010d207dbb Mon Sep 17 00:00:00 2001 From: Tobias Becht Date: Tue, 1 Feb 2022 16:44:08 +0100 Subject: [PATCH 2/3] chore: review feedback, response object --- www/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/helpers.js b/www/helpers.js index 8ab55f0..ed070fe 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -344,7 +344,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, function injectFileEntryHandler(cb) { return function (response) { var fileEntry = createFileEntry(response.file); - // optional: delete response.file; + response.file = fileEntry; response.data = fileEntry; cb(fileEntry, response); } From 43b65fa887f7b921f33d7235d50b704fc2947f7f Mon Sep 17 00:00:00 2001 From: Tobias Becht Date: Tue, 1 Feb 2022 16:48:10 +0100 Subject: [PATCH 3/3] docs: extend downloadFile example code --- README.md | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 7b423ba..f520273 100644 --- a/README.md +++ b/README.md @@ -414,21 +414,32 @@ cordova.plugin.http.uploadFile("https://google.com/", { ``` ### downloadFile -Downloads a file and saves it to the device. Takes a URL, parameters, headers, and a filePath. See [post](#post) documentation for details on what is returned on failure. On success this function returns a cordova [FileEntry object](http://cordova.apache.org/docs/en/3.3.0/cordova_file_file.md.html#FileEntry). +Downloads a file and saves it to the device. Takes a URL, parameters, headers, and a filePath. See [post](#post) documentation for details on what is returned on failure. On success this function returns a cordova [FileEntry object](http://cordova.apache.org/docs/en/3.3.0/cordova_file_file.md.html#FileEntry) as first and the response object as second parameter. ```js -cordova.plugin.http.downloadFile("https://google.com/", { - id: '12', - message: 'test' -}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry) { - // prints the filename - console.log(entry.name); +cordova.plugin.http.downloadFile( + "https://google.com/", + { id: '12', message: 'test' }, + { Authorization: 'OAuth2: token' }, + 'file:///somepicture.jpg', + // success callback + function(entry, response) { + // prints the filename + console.log(entry.name); - // prints the filePath - console.log(entry.fullPath); -}, function(response) { - console.error(response.error); -}); + // prints the filePath + console.log(entry.fullPath); + + // prints all header key/value pairs + Object.keys(response.headers).forEach(function (key) { + console.log(key, response.headers[key]); + }); + }, + // error callback + function(response) { + console.error(response.error); + } +); ``` ### abort @@ -445,12 +456,15 @@ If the request is still in progress, the request's `failure` callback will be in var requestId = cordova.plugin.http.downloadFile("https://google.com/", { id: '12', message: 'test' -}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry) { +}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry, response) { // prints the filename console.log(entry.name); // prints the filePath console.log(entry.fullPath); + + // prints the status code + console.log(response.status); }, function(response) { // if request was actually aborted, failure callback with status -8 will be invoked if(response.status === -8){