0

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?

  • In `downloadFile`, You read the whole file into memory before you send it back to client, try to use stream data over http, check this [question](https://stackoverflow.com/questions/14864939/large-file-transfer-from-http-server-running-java-jersey-rest-api) – MarkoCen Jun 13 '17 at 14:12
  • @MarkoCen The problem is not with sending the file , the Response Object is being sent to the client (Browser/Javascript function) and it shows a status 200 OK . If i were to use the Download File , this will show the download prompt to Open/Save the file, but my Rest Api accepts POST method so I cant use the anchor tag – sudhakarbetha Jun 15 '17 at 07:31

0 Answers0