我正在使用uploadify用于使用Codeigniter上传文件。在上传文件之前,我只需检查文件扩展名是否正确。我试过http://jquery.bassistance.de/也http://forum.jquery.com/jquery - 在上传之前检查文件扩展名
伯尔没有得到正确的结果。任何人都可以告诉我我该怎么办?
在此先感谢...
我正在使用uploadify用于使用Codeigniter上传文件。在上传文件之前,我只需检查文件扩展名是否正确。我试过http://jquery.bassistance.de/也http://forum.jquery.com/jquery - 在上传之前检查文件扩展名
伯尔没有得到正确的结果。任何人都可以告诉我我该怎么办?
在此先感谢...
我想感谢谁张贴答案的人,但他已经删除了帖子。我们可以这样做。
$("#yourElem").uploadify({
'uploader': ...,
'script': ...
'fileExt' : '*.jpg;*.gif;', //add allowed extensions
.....,
'onSelect': function(e, q, f) {
var validExtensions = ['jpg','gif']; //array of valid extensions
var fileName = f.name;
var fileNameExt = fileName.substr(fileName.lastIndexOf('.') + 1);
if ($.inArray(fileNameExt, validExtensions) == -1){
alert("Invalid file type");
$("#yourElem").uploadifyCancel(q);
return false;
}
}
});
感谢您的回答,它果然奏效......
如果你想这样做没有你可以使用下面的一个插件。
的Javascript,使用jQuery:
$(document).ready(function(){
$("#your_form").submit(function(submitEvent) {
// get the file name, possibly with path (depends on browser)
var filename = $("#file_input").val();
// Use a regular expression to trim everything before final dot
var extension = filename.replace(/^.*\./, '');
// Iff there is no dot anywhere in filename, we would have extension == filename,
// so we account for this possibility now
if (extension == filename) {
extension = '';
} else {
// if there is an extension, we convert to lower case
// (N.B. this conversion will not effect the value of the extension
// on the file upload.)
extension = extension.toLowerCase();
}
switch (extension) {
case 'jpg':
case 'jpeg':
case 'png':
alert("it's got an extension which suggests it's a PNG or JPG image (but N.B. that's only its name, so let's be sure that we, say, check the mime-type server-side!)");
// uncomment the next line to allow the form to submitted in this case:
// break;
default:
// Cancel the form submission
submitEvent.preventDefault();
}
});
});
HTML:
<form id="your_form" method="post" enctype="multipart/form-data">
<input id="file_input" type="file" />
<input type="submit">
</form>
你可以做到这一点使用JQuery
HTML
<input type="file" id="FilUploader" />
JQuery的
$("#FilUploader").change(function() {
var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp'];
if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
alert("Only formats are allowed : "+fileExtension.join(', '));
}
});
欲了解更多信息Click Here
而不是使用'split()'将很好地使用'lastIndexOf('。')'来确保您使用的是引用扩展名的最后一个点。 – 2017-01-16 15:55:25
function yourfunctionName() {
var yourFileName = $("#yourinputfieldis").val();
var yourFileExtension = yourFileName .replace(/^.*\./, '');
switch (yourFileExtension) {
case 'pdf':
case 'jpg':
case 'doc':
$("#formId").submit();// your condition what you want to do
break;
default:
alert('your File extension is wrong.');
this.value = '';
}
// $("#upload_" + id).submit();
}
这里是JavaScript验证一个简单的代码,并验证后,将干净的输入文件。
<input type="file" id="image" accept="image/*" onChange="validate(this.value)"/>
function validate(file) {
var ext = file.split(".");
ext = ext[ext.length-1].toLowerCase();
var arrayExtensions = ["jpg" , "jpeg", "png", "bmp", "gif"];
if (arrayExtensions.lastIndexOf(ext) == -1) {
alert("Wrong extension type.");
$("#image").val("");
}
}
如果你不想使用$(this).val()
,你可以试试:
var file_onchange = function() {
var input = this; // avoid using 'this' directly
if (input.files && input.files[0]) {
var type = input.files[0].type; // image/jpg, image/png, image/jpeg...
// allow jpg, png, jpeg, bmp, gif, ico
var type_reg = /^image\/(jpg|png|jpeg|bmp|gif|ico)$/;
if (type_reg.test(type)) {
// file is ready to upload
} else {
alert('This file type is unsupported.');
}
}
};
$('#file').on('change', file_onchange);
希望这有助于!
以下代码允许上传gif,png,jpg,jpeg和bmp文件。
你检查http://jquery.bassistance.de/validate/demo/file_input.html – 2013-02-13 11:12:23
不,它真的很好。您可以告诉我源代码在演示中的位置吗? – V15HM4Y 2013-02-13 11:17:06
http://jquery.bassistance.de/validate/jquery-validation-1.11.0.zip – 2013-02-13 11:19:55