ASP.NET 2.0 FileUpload - Is it possible to set wildcards?

R

ronc85

I'm using ASP.NET 2.0 and C#. With the FileUpload control is it
possible to set Wildcards (e.g., *.xls and *.doc) so that when the
user clicks the Browse button it defaults to Excel and/or Word files?

Thanks,
Sal
 
M

Masudur

I'm using ASP.NET 2.0 and C#. With the FileUpload control is it
possible to set Wildcards (e.g., *.xls and *.doc) so that when the
user clicks the Browse button it defaults to Excel and/or Word files?

Thanks,
Sal

<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Upload File" />&nbsp;<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:RegularExpressionValidator
id="RegularExpressionValidator1" runat="server"
ErrorMessage="Only mp3, m3u or mpeg files are allowed!"
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$"
ControlToValidate="FileUpload1"></asp:RegularExpressionValidator>
<br />
<asp:RequiredFieldValidator
id="RequiredFieldValidator1" runat="server"
ErrorMessage="This is a required field!"
ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
 
D

David Longnecker

Sal-

Here's a post that discusses your concern:

http://groups.google.com/group/micr...3049c8821ec/4546302931d090b2#4546302931d090b2

It appears that, since the Browse window is controled by the host OS, that
it's not something you can manipulate (as you could on a WinForms application).

For a workaround, I'd agree with Ben that the content type is probably the
easiest to check since it's a bit more reliable than checking file extensions.
You can do this by accessing the object's ContentType property.

Pseudo-code:

if (FileUpload1.PostedFile.ContentType != "image/jpeg")
{
// logic here
}

If you have a list of valid content types, you can build a list and iterate
through it. For example:

1. Create a generic list of your valid content types.

private static List<String> ValidContentTypes()
{
List<String> validContentTypes = new List<String>();
validContentTypes.Clear();
validContentTypes.Add("image/gif");
validContentTypes.Add("image/jpeg");
validContentTypes.Add("image/pjpeg");
validContentTypes.Add("image/png");
return validContentTypes;
}

2. And then for your .PostedFile.ContentType:

List<String> _contentTypes = ValidContentTypes();

if (_contentTypes.Exists(delegate(string t) { return FileUpload1.PostedFile.ContentType
== t; }))
{

// Logic here

}



HTH.

-dl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top