0

I want a local file (on the server) to be downloaded by the user. The user first kicks off the file creation by pressing a button and once the file is ready, he should be able to clock on a link or a button to download the file.

Creating the file has not been a problem, as i simply send an AJAX call to my backend which looks like

@POST
@Path("/createFile")
@Produces("application/text")
@Consumes("application/json")
public String createFile(String argsFromPage) {
    /*File creation code here*/
    return "Path of file created";  
}

Now, that the file is created, all I want is to create a link which the user can click and download this file. For now, the file can be either a binary or a CSV file. I have made several attempts but without any success

<button onclick='create_file()'>Create</button>
function create_file() {
    $.ajax({
        method : "POST",
        url : ".path/to/backend/service",
        contentType : "application/json",
        data : JSON.stringify({
            param1 : val1
        })
    }).done(function(data) {
        console.log(data);
    });
}

now once the file has been created, is it possible to create a download link? Better still, is it possible to invoke the download as soon as the file is created? Should this be done in the browser, or the back end?

Follow Up

Once the file has been downloaded, how can i delete it form the server? Is there any way to endure that the file download has been completed?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
AbtPst
  • 7,778
  • 17
  • 91
  • 172
  • Just FYI, a file on the server would be a remote file. There is nothing 'local' about what you're trying to do – Rory McCrossan Apr 24 '17 at 15:10
  • Possible duplicate of [How to trigger a file download when clicking an html button or javascript](http://stackoverflow.com/questions/11620698/how-to-trigger-a-file-download-when-clicking-an-html-button-or-javascript) – cнŝdk Apr 24 '17 at 15:17
  • maybe there's some overlap, but in my case the file creation is dynamic and the download is triggered only after the file has been created. I had looked at the post you mentioned but it dod not solve my problem. Also, there seems to be a some issue with paths in that i cant seem to resolve – AbtPst Apr 24 '17 at 15:32

1 Answers1

1

To create a link to the file you can just create an a element in the DOM within the done() handler. Try this:

function create_file() {
  $.ajax({
    method: "POST",
    url: ".path/to/backend/service",
    contentType: "application/json",
    data: { param1: val1 } // I assume 'val1' is declared in a higher scope?
  }).done(function(path) {
    $('#someContainer').append('<a href="' + path.trim() + '">Click here to download</a>');
  });
}

Note that I removed the manual JSON.stringify call as jQuery will do this for you. Also note that it would be better to return JSON from the AJAX request as it avoids issues with whitespace, although the above should still work given the code sample you provided.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • thanks a lot for this! i can see the download link being created. however when i click on it, i get `Error 404: java.io.FileNotFoundException: SRVE0190E: File not found: /path/to/file`. However, i can see the file at `/path/to/file`. how can i ensure that the browser finds it? Also in my browser i can see `http://localhost:9080/Service_Name/path/to/file`. this seems a bit weird as the file is on the server. Why is the browser looking for the file on the client? am i right? – AbtPst Apr 24 '17 at 15:29
  • 1
    I can't really help with that as it's a path issue and down to how you've set up your server, but it sounds like you're returning a relative path but need to use an absolute one – Rory McCrossan Apr 24 '17 at 15:30
  • thanks, i'll try t figure this out. lastly, is there any way i can delete the file once the user is finished downloading? – AbtPst Apr 24 '17 at 15:35
  • Not specifically. All you can do is delete the file after X number of minutes/hours. You could track how many times the file has been downloaded and delete after a limit it hit, but that is far more involved than your current code base. – Rory McCrossan Apr 24 '17 at 15:38
  • thanks, i tried the full path and now i get `Not allowed to load local resource` – AbtPst Apr 24 '17 at 15:48
  • 1
    That means you're trying to download a file on the local machine which is not allowed. You need to point the download to a server. – Rory McCrossan Apr 24 '17 at 15:51