Osman Villi
Osman Villi

Reputation: 346

Filtering file format

I am using tags in asp.net. I want to use file filtering.(only .tab and .map file format)(during selecting)

my asp.net code:

<ext:FileUploadField ID="BasicField" runat="server" Width="400" Icon="Attach" Text="Örnek Dosya">

    <DirectEvents>
        <Change OnEvent="DosyaSec" IsUpload="true"></Change>
    </DirectEvents>

</ext:FileUploadField>

this output

I dont want to see all files. I want to see *.tab , *.map format.

How can I succeed ?

Upvotes: 0

Views: 446

Answers (2)

Mohamed Badawey
Mohamed Badawey

Reputation: 111

You can validate the file extension like this

<script>

var hash = {
  '.png'  : 0,
  '.jpg' : 1,
};

    var check_extension = function (filename) {
        var re = /\..+$/;
        var ext = filename.match(re);
        if (hash[ext]) {
            alert("valid");
            return true;
        } else {
            alert("Invalid filename, please select another file");

            return false;
        }
    }


</script>


<ext:FileUploadField  ID="FileUploadField1" 
                runat="server" FieldLabel="Photo" ButtonText="" Icon="ImageAdd">
                <Listeners>
                <FileSelected Handler="check_extension(this.value);" />
                </Listeners>
                </ext:FileUploadField>

Upvotes: 1

Koras
Koras

Reputation: 74

You can't directly filter file extension in dialog box. For that you have to use file format validation then you can use Regular Expression or Custom Validation of ASP.NET. Below is example with Regex:

<ext:FileUploadField ID="BasicField" runat="server" Width="400" Icon="Attach" Text="Örnek Dosya"></ext:FileUploadField>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Only .tab or .map files are allowed."
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.tab|.map)$"
ControlToValidate="BasicField">*</asp:RegularExpressionValidator>

Above code help you to check file format as per your need.

Upvotes: 1

Related Questions