admin管理员组文章数量:1333442
I am using Firefox version 14.0.1. I need to filter the upload file window to show up only .txt
files.
My browser is not supporting only for text files(text/plain)
. I can restrict image files by specifying this format ("image/*")
. But I need to filter only text files in File Selector window.
I just want to upload text files(.txt) only and should restrict to other file formats uploading.
Is there any problem with my browser?
I am using Firefox version 14.0.1. I need to filter the upload file window to show up only .txt
files.
My browser is not supporting only for text files(text/plain)
. I can restrict image files by specifying this format ("image/*")
. But I need to filter only text files in File Selector window.
I just want to upload text files(.txt) only and should restrict to other file formats uploading.
Is there any problem with my browser?
Share edited Feb 3 at 5:15 C.HA 2983 silver badges21 bronze badges asked Aug 20, 2012 at 6:45 DevaDeva 1393 silver badges10 bronze badges2 Answers
Reset to default 4Simply do this...
$("#form").submit(function(e) {
e.preventDefault();
var checkUploadFileExtension = $('input[type=file]').val().replace(/C:\\fakepath\\/i, '').split('.').pop();
if(checkUploadFileExtension === 'txt') {
alert("Success Uploaded!");
//Do Something
} else {
alert("Please upload text file only");
//Show error
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form" name="form" method="post">
<input name="file" id="file" type="file" accept="text/*" />
<button class="btn operations-btn btn-default" id="submit">Upload</button>
</form>
I hope, will help my post. Thank you
So according to the MDN reference on the input element accept=[MIME Type]
is unimplemented in Gecko and currently you can only do : accept=image|audio|video
One way you could do this is a server side check for the type on the asp-mvc controller.
But a nicer way would be to do it on the client with some javascript: Something like this:
<script>
function checkExt() {
if(document.mainForm.myfile.value.lastIndexOf(".txt")==-1) {
alert("Please upload only .txt extention file");
return false;
}
}
</script>
<form name="mainForm">
<input type="file" name="myfile" onchange="checkExt();"/>
</form>
See it running here: http://jsfiddle/Egr9V/
If your using JQuery you could make your code slicker or use the Validation plugin as described in this post: How to have jQuery restrict file types on upload?
本文标签: javascriptfilter file upload for only text filesStack Overflow
版权声明:本文标题:javascript - filter file upload for only text files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742290266a2447666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论