5

I want to allow the user to upload images on chrome extension example folder name (upload) and without submit button

<form action="/upload">
  <input type="file" name="myimages" accept="image/*">
</form>

show images

<span class="AvGbtn" id="AvBgIds" style="background-image: url(Here i want to show upload images url
); background-size: 100% 100%;" oncontextmenu="return false">  
</span>
Mukyuu
  • 6,436
  • 8
  • 40
  • 59
Leo
  • 314
  • 1
  • 6
  • 27

1 Answers1

3

$( document ).ready(function() {
    $( '#myimages' ).change(function() {
      var filename = $('input[type=file]').val().replace(/C:\\fakepath\\/i, '');
      $( '#myform' ).submit();
      $( '#uploaded').append('<img src="/upload/' + filename + '"/>');
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myform" action="/upload">
  <input type="file" name="myimages" id="myimages" accept="image/*">
</form>
<div id="uploaded">
</div>

Use the onchange event of the file upload field to submit the form.

EDIT:

Added functionality to display the uploaded image in the div below the form.

Credits: Use jQuery to get the file input's selected filename without the path

Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
  • 1
    Thanks for your help, is ok and with submit button, just i want to show the upload images on this div ` ` – Leo Mar 12 '19 at 13:44
  • so i want to allow user to upload multi images and show all images in that div? i don't know if that is possible or no? – Leo Mar 12 '19 at 13:46
  • 1
    Thanks you, I have not tested that! (health reasons) but I accept it as a correct answer. Thanks again. – Leo Mar 13 '19 at 11:18