1

Input file already have a value and When user drop a file to input[type=file] I want to show a prompt with yes or no.

if yes it will add file.
if no it will do no action and keep old value

$('input[type=file]').on('drop', function (e){
    if( !confirm("are you sure?") ){
    }
});
Gobind Sharma
  • 53
  • 1
  • 1
  • 11

1 Answers1

-1

Use return false or e.preventDefault(); to disable the default action:

$('input[type=file]').on('drop', function(e) {
  if (!confirm("are you sure?")) {
    e.preventDefault();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="test">

Unfortunately, this is apparently not portable. It works in Chrome and Safari, but not FireFox (I'm on a Mac and haven't tried IE).

A portable solution would have to use an HTML modal dialogue to get the confirmation. The cancellation callback can then use the technique in Clearing <input type='file' /> using jQuery to remove the dropped file from the input.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • when you drop a file and press yes if it's not added, i am using firefox – Gobind Sharma Feb 09 '17 at 21:46
  • You're right, it doesn't work in FF. I guess you'll need to implement an HTML modal prompt instead of using `confirm()`. Since the HTML prompt is asynchronous, you can't use `if()`. The callback will need to remove the file if the user says No. See http://stackoverflow.com/questions/1043957/clearing-input-type-file-using-jquery for how to do this. – Barmar Feb 09 '17 at 21:49
  • i want to keep last selected file, and when user choose another file then ask prompt to replace with new file or not – Gobind Sharma Feb 09 '17 at 21:53
  • I think that may be impossible. – Barmar Feb 09 '17 at 21:56