tags:

views:

76

answers:

6

On an HTML page, using the INPUT tag, how can you get it so that when you click the browse button that it filters for image files only? Bonus points if it can include .bmp files.

+1  A: 

In theory, with the accept attribute.

In practice, you can't.

I believe most people who want to do this resort to Flash.

David Dorward
A: 

With the accept attribute, you list the mime types to accept.

<form action="form_action.asp" accept="image/gif, image/jpeg">
    First name: <input type="text" name="fname" /><br />
    Last name: <input type="text" name="lname" /><br />
    Your image: <input type="file" name="pic" id="pic" /><br />
    <input type="submit" value="Submit" />
</form>

Taken from here.

Cesar
This is ignored by almost all webbrowsers.
BalusC
yep, i just tried their example and it doesnt work. This answer was very helpful though. Although David deserves the points for this he already has 36k points!
djangofan
A: 

Use the "accept" attribute on your input tag.

E.g.:

<input type="file" name="pic" id="pic" accept="image/gif, image/jpeg" />

EDIT: On further reading, it seems this isn't properly supported by any major browsers and therefore shouldn't be used. Instead it look like you'll need to use some sort of server-side or JavaScript validation instead.

fat_tony
A: 

What you're talking about is the accept attribute, which unfortunately, isn't supported by any browser.

Wyatt
+1  A: 

The accept attribute of the HTML <form> element is meant for that, but this optional attribute is ignored by almost all webbrowsers. The answer is Flash or Java Applet. For both there exist 3rd party programs. E.g. Uploadify, SWFUpload and JumpLoader. Uploadify has my recommendation.

BalusC
A: 

Supposedly, you should use the "accept" attribute with the correct MIME type like so:

<input type="file" name="pic" id="pic" accept="image/bmp" />

Sadly, this isn't currently supported by any browsers. You can however validate the file using javascript afterwards. Here is a useful link with code: http://www.cs.tut.fi/~jkorpela/forms/file.html#filter

Hope this helps!

Joe Axon