Retsam
Retsam

Reputation: 33389

JQuery - Prompt for file then process it

So my goal is to have a button; when the user clicks the button, they're prompted for a file, and then the program does something with that file. (Probably sending it off through an AJAX request to be processed) Currently I have the following implementation, with a hidden form and a button that invokes a click on that form.

<script>
   $(document).ready(function () 
   {
       $("#upload_button").click(function() { 
           $("#file_upload").click();
       });
   });
</script>

<input type="file" style="display:none" id="file_upload" />
<button id="upload_button">Upload File</button>

With this, clicking the button prompts the user for a file, as intended, but I don't know how to insert code to run after the user chooses a file. If I put lines after $("#file_upload").click();, it'll just run synchronously with the user uploading.

I realize I could obviously put a second button in, but two buttons seems unnecessary, when a single one would really do.

Upvotes: 1

Views: 759

Answers (1)

jacksonfiverniner
jacksonfiverniner

Reputation: 92

  $('input[type=file]').change(function() { 
      //do something!
  });

Upvotes: 2

Related Questions