Amit
Amit

Reputation: 7035

keep file dialog open even after displaying alert

HI Guys,

I have the below code where I check for the file types of the file selected.
hiddenFileInputFile is of file input type.
Below code is working fine but if the user does not supply a .csv file then it show the alert, which is fine, but then I want the file dialog to remain open which is not happening.

Please advise.
Also is there any way where I can apply the filter directly to the file dialog to select only .csv files using jquery/javascript?

Element.find('input.hiddenFileInputFile').change(function () 
{
            var filename = $(this).val();
            if (!/\.csv$/.test(filename)) {
                alert('Please select a csv file');
                return false;
            }
});

Thanks.

Upvotes: 0

Views: 432

Answers (2)

omerkirk
omerkirk

Reputation: 2527

Unfortunately you can't externally trigger the file input click event for security reasons. However if you want to restrict the user on the client side you can try:

<input type="file" accept="text/csv" /> 

This will only make the .csv type files visible in the open file dialog. However I would put a control on the server side as well. If not the correct type you can open an alert dialog and style the file input accordingly (e.g. red border) and let the user to open the file dialog because there isn't any way for you to trigger that.

There are ways provided by some plugins for jquery which give you a lot of control on the uploading process.

Jquery plugin 1, uploadify jquery

to name a few.

Hope it helps.

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

Just invoke its click event after the alert:

if (!/\.csv$/.test(filename)) {
   alert('Please select a csv file');
   $(this).click();
   return false;
}

This will not "keep it open" which is not possible, but re-open it when the file is invalid.

Upvotes: 1

Related Questions