title: Upload Image with input file button or drag and drop the file
body: <p>now I am learning to upload file with HTML5 and JavaScript (JQuery). and I got a problem with it. so, bellow here is the preview of my app.</p>
  
  <p><a href="https://i.stack.imgur.com/zM3sF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zM3sF.png" alt="enter image description here"></a></p>
  
  <p>and from this app, I want to upload file either you choose file from input button or drag and drop the file in the drop zone.</p>
  
  <p>here is the code :</p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;div id="blah" class="col-lg-4 dropzone" ondrop="drag_drop(event)" ondragover="return false"&gt;
          &lt;p&gt;Drop Your Image Here&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="col-lg-3"&gt;
          &lt;?php echo form_open_multipart('umum/uploadFile', ['id' =&gt; 'form_upload']);?&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="userfile"&gt;File input&lt;/label&gt;
                  &lt;input type="file" id="userfile" name="userfile"&gt;
                  &lt;p class="help-block"&gt;Only image file allowed to upload here.&lt;/p&gt;
              &lt;/div&gt;
              &lt;button class="btn btn-primary" onclick="uploadFile()"&gt;Upload&lt;/button&gt;
          &lt;?php echo form_close(); ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and then my script :</p>
  
  <pre><code>&lt;script&gt;
          // preview the file that will be uploaded
          function readURL(input) {
              if (input.files &amp;&amp; input.files[0]) {
                  var reader = new FileReader();
  
                  reader.onload = function(event) {
                      $('#blah').html('&lt;img src="'+event.target.result+'" alt="your image"&gt;');
                  }
  
                  reader.readAsDataURL(input.files[0]);
              }
          }
  
          // manual select file from input file button
          $("#userfile").change(function(){
              readURL(this);
          });
  
          // drag and drop file image
          $(function() {
              $('#blah').bind('dragover', function() {
                  $(this).addClass('dropzone-active');
              });
              $('#blah').bind('dragleave', function() {
                  $(this).removeClass('dropzone-active');
              });
          });
  
          function drag_drop(event) {
              event.preventDefault();
  
              // parsing data to preview the file before upload
              readURL(event.dataTransfer);
  
              // alert(event.dataTransfer.files[0]);
              // alert(event.dataTransfer.files[0].name);
              // alert(event.dataTransfer.files[0].size+" bytes");
          }
      &lt;/script&gt;
  </code></pre>
  
  <p>and the problem is I cannot change the value of the input file button when I drag and drop the image in the drop zone. and of course, I cannot upload the file because the input file remains empty. Btw, I have tried to write <code>$('#userfile').val(event.dataTransfer.files[0]);</code> in drag_drop function up there and the result in my console is :</p>
  
  <blockquote>
    <p>Uncaught DOMException: Failed to set the 'value' property on
    'HTMLInputElement': This input element accepts a filename, which may
    only be programmatically set to the empty string.</p>
  </blockquote>