javascriptjqueryhtmlweb-services

How to download file after some task


Hi I am trying to download file through unusual process.

The situation is as follows:

  1. a user trigger download button.

  2. the user select download directory.

  3. web browser aggregate whole information of the user from third-party server

  4. when the aggregating is completed the user will download the information automatically.

The problem is that download destination is determined before starting of download.

Is there any method to treat this issue?

Thank you in advance.


Solution

  • You can't set the distention folder. But as far as I understand your question is not the big issue but the downloading after you end the process.

    If so, the steps will be: the user click, the data will send, the saved folder will be chosen by the user, the download will be start.

    So, to start a download when you want (after the data will sent), you can create a hidden link with download attribute then, click on it whenever you want.

    Like this: (The downloaded file is demy of course, you need to set the link, the real URL).

    function startDownload() {
      $('#status').html('collect data..')
      setTimeout(function(){
        download('test content', 'file name.txt', 'text/plain');
      }, 2000);
    }
    
    function download(text, name, type) {
      var file = new Blob([text], {type: type}) 
      $('<a href="' + URL.createObjectURL(file) + '" download></a>').get(0).click();
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button onclick="startDownload()">Download</button>
    <div id="status"></div>

    http://output.jsbin.com/sihibu