How do you get a HTML Browse button to filter and show only images?

后端 未结 6 2056
星月不相逢
星月不相逢 2021-01-21 09:31

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 fil

相关标签:
6条回答
  • 2021-01-21 09:59

    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!

    0 讨论(0)
  • 2021-01-21 10:00

    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.

    0 讨论(0)
  • 2021-01-21 10:01

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

    0 讨论(0)
  • 2021-01-21 10:12

    In theory, with the accept attribute.

    In practice, you can't.

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

    0 讨论(0)
  • 2021-01-21 10:22

    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.

    0 讨论(0)
  • 2021-01-21 10:25

    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.

    0 讨论(0)
提交回复
热议问题