How to open select file dialog via js?

前端 未结 9 1718
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 06:16
$(\'#id\').click();

It doesn\'t work on Chrome 26 on Mac OS.

The problem actually is creation "upload" widget that can be integrated in

9条回答
  •  别那么骄傲
    2021-01-30 07:18

    To expand on the answer from 'levi' and to show how to get the response from the upload so you can process the file upload:

    selectFile(event) {
        event.preventDefault();
    
        file_input = document.createElement('input');
        file_input.addEventListener("change", uploadFile, false);
        file_input.type = 'file';
        file_input.click();
    },
    
    uploadFile() {
        let dataArray = new FormData();
        dataArray.append('file', file_input.files[0]);
    
        // Obviously, you can substitute with JQuery or whatever
        axios.post('/your_super_special_url', dataArray).then(function() {
            //
        });
    }
    

提交回复
热议问题