Skip to content

Commit 87c915f

Browse files
authored
Merge pull request #451 from h4ck-rOOt/download-response-ext
feat: response object on file download
2 parents 5b8f20e + 43b65fa commit 87c915f

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,32 @@ cordova.plugin.http.uploadFile("https://google.com/", {
414414
```
415415

416416
### downloadFile<a name="downloadFile"></a>
417-
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).
417+
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.
418418

419419
```js
420-
cordova.plugin.http.downloadFile("https://google.com/", {
421-
id: '12',
422-
message: 'test'
423-
}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry) {
424-
// prints the filename
425-
console.log(entry.name);
426-
427-
// prints the filePath
428-
console.log(entry.fullPath);
429-
}, function(response) {
430-
console.error(response.error);
431-
});
420+
cordova.plugin.http.downloadFile(
421+
"https://google.com/",
422+
{ id: '12', message: 'test' },
423+
{ Authorization: 'OAuth2: token' },
424+
'file:///somepicture.jpg',
425+
// success callback
426+
function(entry, response) {
427+
// prints the filename
428+
console.log(entry.name);
429+
430+
// prints the filePath
431+
console.log(entry.fullPath);
432+
433+
// prints all header key/value pairs
434+
Object.keys(response.headers).forEach(function (key) {
435+
console.log(key, response.headers[key]);
436+
});
437+
},
438+
// error callback
439+
function(response) {
440+
console.error(response.error);
441+
}
442+
);
432443
```
433444

434445
### abort<a name="abort"></a>
@@ -445,12 +456,15 @@ If the request is still in progress, the request's `failure` callback will be in
445456
var requestId = cordova.plugin.http.downloadFile("https://google.com/", {
446457
id: '12',
447458
message: 'test'
448-
}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry) {
459+
}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry, response) {
449460
// prints the filename
450461
console.log(entry.name);
451462

452463
// prints the filePath
453464
console.log(entry.fullPath);
465+
466+
// prints the status code
467+
console.log(response.status);
454468
}, function(response) {
455469
// if request was actually aborted, failure callback with status -8 will be invoked
456470
if(response.status === -8){

www/helpers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
343343

344344
function injectFileEntryHandler(cb) {
345345
return function (response) {
346-
cb(createFileEntry(response.file));
346+
var fileEntry = createFileEntry(response.file);
347+
response.file = fileEntry;
348+
response.data = fileEntry;
349+
cb(fileEntry, response);
347350
}
348351
}
349352

0 commit comments

Comments
 (0)