0

I am trying to downloada zip file sent from a REST by adding the Content-Type and the Content-Disposition doing the following:

Server-side

@Produces("application/x-zip-compressed")
    public Response export(@PathParam("username") String username,
            @Context UriInfo ui) {

        long timeStamp = new Date().getTime();
        String exportedFolder = "/home/madmin/pods/"+username+"/download/";

        String Path = ui.getRequestUri().toString()
                .replace(replaceMe + username, "");

        Path = Path.replace("http://", "https://");


        Path = Path.replace(",system/export", "");



        String outputZipFile = exportedFolder+"exported_on_"+timeStamp+".zip";
        String sourceFolder = null;

        File file = new File(exportedFolder);
        if (!file.exists()) {
            if (file.mkdirs()) {
                System.out.println("Directory is created!");
            } else {
                System.out.println("Failed to create directory for exporting!");
                //return ;
            }
        }

        constructGraphs cGraphs = new constructGraphs(Path, username);
        sourceFolder = cGraphs.writeGraphFiles();

        generateZipFile appZip = new generateZipFile(sourceFolder,outputZipFile);
            appZip.generateFileList(new File(sourceFolder));
            appZip.zipIt(outputZipFile);

            //Read the outputZipFile as inputStream and return it

            FileInputStream fileIs = null;
        try {
            fileIs = new FileInputStream(outputZipFile);
        } catch (IOException e) {
            throw new WebApplicationException(404);
        }

        String fileName= outputZipFile.substring(outputZipFile.lastIndexOf("/")+1, outputZipFile.length());

        return Response.status(Status.OK).entity(fileIs).header("Content-Disposition","attachment; filename = "+fileName).build();
    }

Client-side:

The client side on the other hand is expecting application/x-zip-compressed as follows:

$http({
        method: 'GET',
        url: uri,
        headers: {
          'Accept': 'application/x-zip-compressed'
        },
        withCredentials: true
      }).
      success(function(data, status, headers) {
        if (status == 200 || status == 201) {
          notify('Success', 'Node exported.');
        }
      }).
      error(function(data, status) {
        if (status == 401) {
          notify('Forbidden', 'Authentication required to edit the resource.');
        } else if (status == 403) {
          notify('Forbidden', 'You are not allowed to edit the resource.');
        } else {
          notify('Failed', status + " " + data);
        }
      });

The file is not popping up, instead I see the input stream in the response.

Is there anything wrong I am doing here? Any help would be really appreciated.

Meiko Rachimow
  • 4,196
  • 2
  • 21
  • 41
mzereba
  • 2,217
  • 5
  • 24
  • 39
  • try this response.setContentType("application/x-zip-compressed") before returning response – Saurabh Mar 19 '16 at 10:29
  • @userRaj checking the response it is set by default in the header, that's why I didn't have to do it explicitly. – mzereba Mar 19 '16 at 11:41

1 Answers1

0

It is not possible with AJAX and I assume you are using angularjs http serivce in this case. See: Why threre is no way to download file using ajax request?.

You have different options to solve your problem. For instance: easiest way to open a download window without navigating away from the page or How do you serve a file for download with AngularJS or Javascript?

Community
  • 1
  • 1
Meiko Rachimow
  • 4,196
  • 2
  • 21
  • 41