简体   繁体   中英

File upload is not triggering on confirm using jquery

I have the following code and it does not work in Chrome if I keep the trigger function inside the confirm function but it works in Firefox and Edge.

 fileInput = $('<input type="file" id="upload-file" name="timing-upload" style="display:none;"/>'); if (confirm("Are you sure you want to replace all files?")) { $(fileInput).trigger('click'); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

But it works if we remove the confirm condition in Chrome.

 fileInput = $('<input type="file" id="upload-file" name="timing-upload" style="display:none;"/>'); $(fileInput).trigger('click'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

There is already a similar question Triggering a input file click does not bring the file upload dialog but not any satisfied answer.

Any help is highly appreciated. Thanks in advance.

I'm not sure but according to the confirm documentation I think that this is the reason that the code doesn't work in chrome:

Starting with Chrome 46.0 this method is blocked inside an <iframe> unless it sandbox attribute has the value allow-modal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM