I have a large file to download (~800MB) from my Server, a jersey REST API which returns a 'response' object on POST method.
I have the code working on client (browser/javascript) to download the file using Angularjs $http service to invoke the POST method on the API.
However, what I see is that the download dialog on the browser (firefox) shows up only after the response is completely downloaded. It does not show the download dialog at the beginning of the file download and I keep the user waiting for almost 3-4 mins for the dialog to turn up :( which is not intended
Here is my Jersey API Response,
@POST
@Path("downloadFile")
@Consumes(MediaType.APPLICATION_JSON)
public Response downloadFile(StorageInputs storageInputs) {
StorageHelper storageHelper=new StorageHelper(storageInputs);
String file = (String) storageInputs.getAdditionalProperties().get("storage_file");
return storageHelper.downloadFile(file);
}
My View has the below
<a ng-click="downloadFile(file)"><i class="fa fa-download"></i></a>
My Javascript has,
$scope.downloadFile = function (file) {
var server = "http://localhost:8080/myserver/myapi";
var fileName = file.split("/").pop();
var body = { "storage_file": file};
$http({
method: 'POST',
url: server + "/downloadFile",
headers: {'Content-Type': 'application/json'},
data: body,
responseType: 'blob'
})
.then(function successCallback(response) {
var contentType = response.headers('content-type');
console.log("Headers Content-Type=" + contentType);
console.log("Headers Content-Length=" + response.headers('Content-Length'));
console.log("Headers ETag=" + response.headers('ETag'));
var blob = new Blob([response.data], {type: contentType});
FileSaver.saveAs(blob, fileName);
}, function errorCallback(response) {
console.log("Error Status = " + response.status);
console.log("Error = " + response.data);
});
};
On the Browser Developer Tools, I see,
POST call shows 200OK but waits for the response to be downloaded for 3 mins, and then shows the download dialog for the user asking for "Save or Open" the file.
Also, the console.log above for the headers in the response are printed at the end after the response is downloaded and before the download dialog is shown.
Is there a way I can ensure the download dialog ("save or Open") can be shown before the response is downloaded?