Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get script to wait for file dialogue to return

I have a web page where I want to prompt the user for a file to upload, but I don't want to display the <input type="file"/> element.

I have a button that triggers the file dialogue to display, but the code doesn't wait for the dialogue to return.

Is there an event I can hook into that is raised when the file dialogue returns? Is there some other thing I've not thought of?

This is what I have at the moment, it uses an alert to block the code. I want something less hacky.

function importValues(e)
{
    var f = document.getElementById('file');
    f.click();
    alert('loading'); //hack to make the code wait for the user to choose a file before making the ajax call
    var formdata = new FormData(jQuery('#frmImport')[0]);
    jQuery.ajax({
        url: 'importFile',
        type: 'POST',
        data: formdata,
        chache: false,
        contentType: false,
        processData: false,
    });
}
like image 282
Matt Ellen Avatar asked Oct 05 '12 08:10

Matt Ellen


Video Answer


1 Answers

i think you need to refactor your code and handle native file upload events.
if you have some element like file dialog

<input type="file" name="file" id="file" /> 

then you can hook up to its events and specially to onchange

$(function(){
    $("#file").change(function(e){ alert('selected') });
});​

jsFiddle

like image 198
Sagiv Ofek Avatar answered Oct 20 '22 03:10

Sagiv Ofek