From 43b65fa887f7b921f33d7235d50b704fc2947f7f Mon Sep 17 00:00:00 2001 From: Tobias Becht Date: Tue, 1 Feb 2022 16:48:10 +0100 Subject: [PATCH] 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){