在HTML文件打开对话框中过滤文件 [英] Filter files in HTML file open dialog

查看:171
本文介绍了在HTML文件打开对话框中过滤文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTML标记将文件上传到服务器:

 < input type =file> 

当我点击浏览按钮时,它会显示一个文件打开对话框。我可以通过将扩展过滤器传递到该对话框来过滤文件吗?就像我们可以在.Net框架的文件打开对话框中做的那样,通过传递如下内容:

 文本文件* .txt | .txt 

使用这个过滤器我们只能打开 .txt 文件。其他文件未向用户显示。有没有这个对话框的选项?

解决方案

标准



在HTML5中,您可以设置接受属性,所以现在可以! W3C标准规定:


可以指定 accept 属性为用户代理提供提示的文件类型将被接受。




接受的值[ Wikipedia中的完整列表]



只需传递有效的 MIME类型到属性,例如:
$ b


  • audio / *

  • video / *

    • video / ogg


  • image / *

    • image / jpg

    • image / bmp



  • < h2>示例代码

     < input type =fil eaccept =image / *> 


    I am working with file upload to the server using HTML tag:

    <input type="file">
    

    When I click on the browse button it shows me a file open dialog. Can I filter the files by passing the extension filter to that dialog? Like we can do in .Net framework's file open dialog by passing some thing like:

    Text files *.txt|.txt
    

    Using this filter we can only open .txt files. Other files not shown to the user. Is there any option for this dialog?

    解决方案

    Standard

    Actually in HTML5 you can set the accept attribute so now this is possible! The W3C standard states:

    The accept attribute may be specified to provide user agents with a hint of what file types will be accepted.

    Accepted values [Full list in Wikipedia]

    Just pass a valid MIME type to the attribute for example:

    • audio/*
    • video/*
      • video/ogg
    • image/*
      • image/jpg
      • image/bmp

    Example code

    <input type="file" accept="image/*">
    

    这篇关于在HTML文件打开对话框中过滤文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆