Trigger file input dialog

前端 未结 8 2087
忘了有多久
忘了有多久 2021-02-13 04:31

How can I auto trigger file input? ie. in the link below I want to trigger upload button on load

DEMO

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-13 05:00

    It is not possible to programically open "Open File" dialog utilizing javascript without user action ; see Trigger click on input=file on asynchronous ajax done() .

    Could, alternatively, create an element to overlay html at document .ready() event to provide user with options to click to open "Open File" dialog by calling click on input type="file" element , or close overlay of html by clicking "Close" .

    $(function() {
    
      function openFileDialog() {
        button.fadeTo(0,1).find(input)[0].click();
        dialog.hide();
      }
    
      function closeDialog() {
        dialog.hide();
        button.fadeTo(0,1);
      }
    
      var input = $("input[type=file]")
    
      , button = $("#button").on("click", function(e) {
        e.stopPropagation();
        this.firstElementChild.click()
      })
    
      , options = $("
    input {
      width: 0;
      opacity: 0;
    }
    
    #button {
      position: relative;
      font-size: 32px;
      width: 150px;
      left: 32vw;
      opacity: 0;
    }
    
    
      

提交回复
热议问题