search

Home  >  Q&A  >  body text

How to keep the last selected file when deselecting <input type="file">

After selecting the file for the first time, click the button again. After the dialog box pops up, you do not select the file but choose to cancel. The file selected for the first time is gone. . . Showing no files selected

天蓬老师天蓬老师2727 days ago1976

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-07-05 11:10:02

    Try this https://stackoverflow.com/que...

    reply
    0
  • 怪我咯

    怪我咯2017-07-05 11:10:02

    If you have special needs, you can use a temporary variable to save the value of the last operation, for example, after the file selection event is triggered

    var prevFile = null;
    
    fileElement.onchange = function(){
        var file = fileElement.files[0] || prevFile;
        if(!file) return false;
        
        ///此处省略一大段逻辑///
        prevFile = file;
    };
    

    reply
    0
  • Cancelreply