Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Second use of input file doesn't trigger onchange anymore

I have a picture upload button that automatically uploads the file when selected from the browse window.
Nonetheless, it doesn't work the second time around and that is because the onChange event is not triggered. Why is that so?

Here is a use case:
1. On click of the button, I call uploadMessagePicture(), which opens the browse window.
2. When the user has selected a picture, a onChange event is detected which triggers the ajaxFileUpload() and shows the photo editor area.
3. When the upload is complete, a preview of the image is shown. 4. The user deletes the picture by calling deleteMessageImage() (which also clears the fields and hides the photo editor).
5. The user tries to upload another picture.

At this point, the browse window appears, but when I select a picture, the onChange event is not triggered so nothing happens... (Also, it doesn't seem like the filename is even transfered to the input' value field.)
I'm on Firefox 23.0.1

Here is the HTML:

<a class="im-photo-btn" href="javascript:;" onclick="javascript:uploadMessagePicture();"><i class="icon-join-photo"></i></a>  <div class="editor-photoarea" id="editor-photoarea" style="display:none;">     <input name="image_message_file" type="hidden" id="image_message_file" value="" />     <div id="image_message_upload">         <input name="image-message" type="file" id="image-message" style="display:none; visibility:hidden;" />          <!-- hide it and trigger it from the photo btn (need both display:none + visibility:hiden for browser compatibility) -->     </div>     <div class="loading" id="image_message_loading" style="display:none;"><img alt="<?php echo $lang["Loading"];?>" src="lib/immedia/img/ajax-loader.gif" /> <?php echo $lang["Loading"];?></div>     <div class="preview" style="display:none;" id="image_message_preview">         <!-- <div>Image preview :</div>         <div id='small_message_msg'></div>             <img src='' />             <div class='btnoptions'>                 <a onclick='javascript:deleteMessageImage();' href='javascript:;' class='button' title="Delete photo">                     <span class='ui-icon ui-icon-closethick smsg'></span>Delete                 </a>          </div>--> </div> 

Here is the javascript to automatically upload the picture

$(document).ready(function (){     $("#image-message").on('change', function () {         $('#editor-photoarea').show();         ajaxFileUploadMessagePicture();     }); });  function uploadMessagePicture(){     //trigger the browse click     if($('#image_message_file').val() == ''){         //let the person upload a file only if none are there         $('#image-message').trigger('click');     } }  function ajaxFileUploadMessagePicture(){ $("#msg_error_no_image_message").hide();  var imageName = $("#image-message").val(); if(imageName != ''){     $("#image_message_loading").show();     $('#sendBtn').attr('disabled', 'disabled');      $.ajaxFileUpload     (         {             url:siteRegionDir+'lib/immedia/uploadMessagePicture.php',             secureuri:false,             fileElementId:'image-message',             dataType: 'json',             success: function (data, status)             {                 updateMessagePicture(data);                          },             error: function (data, status, e)             {                 alert(e);             }         }     )    }else{     $("#msg_error_no_image_message").show(); } return false; } 

Here is the Javascript to delete the picture and clear the fields

function deleteMessageImage(){ //delete the image var file = $("#image_message_file").val();  if(file != '') {     $.post(siteRegionDir+'lib/immedia/deleteMessagePicture.php',{filename:file}, function(data) {                clearPictureAttachment();                            $('#editor-photoarea').hide();//make sure it is hidden         },"html"       ); }else{           clearPictureAttachment();     $('#editor-photoarea').hide();//make sure it is hidden } }  function clearPictureAttachment(){ //var control = $("#image-message"); $("#image-message").attr('value', ''); //control.replaceWith( control = control.clone( true ) );//so it resets it all, truw = keep the bound events $("#image_message_file").attr('value', ''); $("#image_message_loading").hide(); $("#image_message_upload").show(); $("#image_message_preview").hide(); } 

Any help would be great! Thanks!

like image 720
ether Avatar asked Oct 28 '13 19:10

ether


People also ask

How to get rid of jQuery file input error onchange?

It happens with file input onchange event (using JavaScript) and with jQuery $ ('#file').change (function () { } event. A simple solution to get rid of this issue will be to clear the file input value. I’ll show you how. Let’s see the bug (or the issue) first.

How to fire the event twice for the same input?

Here is the working code, what you need to do is to clone the file input. var control = $('input[type=file]'); control.replaceWith( control = control.clone( true ) ); and the event will still be fired on second time.

When does onchange trigger when typing a text field?

Consider this example. You are writing the word PLEASE into a text field. Typing each individual letter P-L-E-A-S-E does not trigger OnChange. Instead, it triggers when the value changes from BLANK () to PLEASE.

When does onchange trigger when the value changes?

Instead, it triggers when the value changes from BLANK () to PLEASE. OnChange – How the app responds when the user changes the value of a control (for example, by adjusting a slider). Wouldn't it be annoying if OnChange triggered as you were dragging the slider to its destination?


2 Answers

If you're using React or just javascript in general you can what you can also do is "reset" the value of the event target.

So If you have something like a component that can upload and "remove" an image:

<input type="file" onChange={onChange} /> 

your onChange can be:

onChange(event) {      const files = event.target.files;      // your logic here on what to do with files (maybe fetch the preview or something)      // this line right below will reset the      // input field so if you removed it you can re-add the same file     event.target.value = '' } 
like image 66
I am L Avatar answered Oct 21 '22 15:10

I am L


My guess is that you test your functionality with the same picture file over and over again.

If you select the same file, the onChange event of input[type=file] will not fire.

Different file with the same name would not fire the event as well. Select a different image file - it will happen.

To avoid this you can reset the form to ensure that choosing a file will be performed on a clean file control

like image 35
Sych Avatar answered Oct 21 '22 13:10

Sych