Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input File upload success?

I have a quite complex form that has one "file" input, such as:

<input type="file" />

Is it possible to trigger some kind of "upload successful" with JS, so after user uploads the file the whole form submits itself?

like image 359
user3245449 Avatar asked Nov 27 '25 06:11

user3245449


1 Answers

You can upload your files asynchronously (if you want you may upload multiple files). So for example:

<input type="file" class="js-file-upload" multiple>

After creating input tag, you can listen to change event

$(".js-file-upload").on("change", function(e){
 var files = e.target.files;
 // your own logic to filter files etc.
 // upload your file
});

After all this you can listen to upload finish event (to submit after loading all files for example). Javascript XMLHttpRequest object provides two events: upload.progress and load. So use it ))

Pseudocode from my project:

file.on("upload.progress", function(e){
  //value show progress
  var value = Math.floor(e.loaded / e.total * 100);
});
file.on("load", function(response){ //logic });
like image 89
Farkhat Mikhalko Avatar answered Nov 29 '25 21:11

Farkhat Mikhalko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!