我可以使用html input元素的accept属性来指定要上传的文件类型吗?

发布于 2025-01-06 19:06:13 字数 206 浏览 2 评论 0原文

大家好,我想验证在服务器上上传到 .txt 扩展名的文件类型。我可以使用输入元素的接受属性来过滤文件类型吗?如果是的话我该怎么做,如果还有其他方法吗?

一些代码是

   <input type="file"     name="uploadfile" value="" size="50" /> 

谢谢

hello everyone i want validate the type of file being uploaded on sever to .txt extension. Can i use accept attribute of input element to filter out the type of file. If yes how can i do that and if any other way ??

Some code is

   <input type="file"     name="uploadfile" value="" size="50" /> 

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪化雨蝶 2025-01-13 19:06:13

这是javascript自定义验证

var files = document.getElementsByName("uploadfile");
if(files[0].value.indexOf(".txt") == -1) {
// Code placed here will be executed if an invalid file type is found.
}

,是的,您也可以使用accept属性:

<input accept="audio/*|video/*|image/*|MIME_type" />

但是据我所知,IE和Safari不支持它,

但请确保也在服务器端验证上传的文件。仅客户验证是不够的。

here is javascript custom validation

var files = document.getElementsByName("uploadfile");
if(files[0].value.indexOf(".txt") == -1) {
// Code placed here will be executed if an invalid file type is found.
}

And yes you can use accept attribute as well:

<input accept="audio/*|video/*|image/*|MIME_type" />

However as far as I know it is not supported by IE and Safari

But please make sure to validate the uploaded file on the server side as well. Just client validation is not enough.

年华零落成诗 2025-01-13 19:06:13

你好,在普通的 html 中你不能这样做,
你需要闪存支持。

最好使用uploadify插件

http://www.uploadify.com/

Hi in normal html you can't do that ,
you need flash support for this .

Better use the uploadify plugin

http://www.uploadify.com/

大姐,你呐 2025-01-13 19:06:13

是的,您可以使用 accept 属性。浏览器支持各不相同。它采用 Internet 媒体类型(MIME 类型)列表作为值。对于纯文本(通常是 .txt 文件),则为 accept="text/plain"。通过将选择限制为浏览器视为纯文本文件的文件,这可能会或可能不会影响文件选择功能。

Yes, you can use the accept attribute. Browser supports varies. It takes an Internet media type (MIME type) list as value. For plain text (which is that .txt files usually are), that would be accept="text/plain". This may or may not affect the file selection functionality by restricting the choices to files that the browser regards as plain text files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文