I am using uploadify for uploading files with Codeigniter. And before uploading the file I just have to check whether the file extension is correct is correct or not. I have tried with http://jquery.bassistance.de/ and also http://forum.jquery.com/
我使用uploadify上传Codeigniter文件。在上传文件之前,我只需要检查文件扩展名是否正确是否正确。我试过http://jquery.bassistance.de/和http://forum.jquery.com/
Bur didn't got proper result. Can anyone please tell me how can I do the same?
Bur没有得到适当的结果。任何人都可以告诉我,我怎么能这样做?
Thanks in advance...
提前致谢...
8 个解决方案
#1
37
If you want to do it without a plugin you could use the following.
如果你想在没有插件的情况下这样做,你可以使用以下内容。
Javascript, using jQuery:
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>
#2
25
You can achieve this using JQuery
您可以使用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(', '));
}
});
For more info Click Here
欲了解更多信息请点击这里
#3
13
I would like to thank the person who posted the answer, but he has deleted the post. We can do it like this.
我要感谢发布答案的人,但他已删除了帖子。我们可以这样做。
$("#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;
}
}
});
Thanks for the answer, it really worked...
谢谢你的答案,它确实有效......
#4
6
Here is a simple code for javascript validation, and after it validates it will clean the input file.
这是一个简单的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("");
}
}
#5
1
If you don't want to use $(this).val()
, you can try:
如果您不想使用$(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);
Hope this helps!
希望这可以帮助!
#6
0
The following code allows to upload gif, png, jpg, jpeg and bmp files.
以下代码允许上传gif,png,jpg,jpeg和bmp文件。
var extension = $('#your_file_id').val().split('.').pop().toLowerCase();
if($.inArray(extension, ['gif','png','jpg','jpeg','bmp']) == -1) {
alert('Sorry, invalid extension.');
return false;
}
#7
0
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 = '';
}
}
#8
0
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function(){
jQuery('.form-file input').each(function () {
$this = jQuery(this);
$this.on('change', function() {
var fsize = $this[0].files[0].size,
ftype = $this[0].files[0].type,
fname = $this[0].files[0].name,
fextension = fname.substring(fname.lastIndexOf('.')+1);
validExtensions = ["jpg","pdf","jpeg","gif","png","doc","docx","xls","xlsx","ppt","pptx","txt","zip","rar","gzip"];
if ($.inArray(fextension, validExtensions) == -1){
alert("This type of files are not allowed!");
this.value = "";
return false;
}else{
if(fsize > 3145728){/*1048576-1MB(You can change the size as you want)*/
alert("File size too large! Please upload less than 3MB");
this.value = "";
return false;
}
return true;
}
});
});
});
</script>
</head>
<body>
<form>
<div class="form-file">
<label for="file-upload" class="from-label">File Upload</label>
<input class="form-control" name="file-upload" id="file-upload" type="file">
</div>
</form>
</body>
</html>
Here is complete answer for checking file size and file extension. This used default file input field and jQuery library. Working example : https://jsfiddle.net/9pfjq6zr/2/
这是检查文件大小和文件扩展名的完整答案。这使用了默认文件输入字段和jQuery库。工作示例:https://jsfiddle.net/9pfjq6zr/2/
#1
37
If you want to do it without a plugin you could use the following.
如果你想在没有插件的情况下这样做,你可以使用以下内容。
Javascript, using jQuery:
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>
#2
25
You can achieve this using JQuery
您可以使用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(', '));
}
});
For more info Click Here
欲了解更多信息请点击这里
#3
13
I would like to thank the person who posted the answer, but he has deleted the post. We can do it like this.
我要感谢发布答案的人,但他已删除了帖子。我们可以这样做。
$("#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;
}
}
});
Thanks for the answer, it really worked...
谢谢你的答案,它确实有效......
#4
6
Here is a simple code for javascript validation, and after it validates it will clean the input file.
这是一个简单的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("");
}
}
#5
1
If you don't want to use $(this).val()
, you can try:
如果您不想使用$(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);
Hope this helps!
希望这可以帮助!
#6
0
The following code allows to upload gif, png, jpg, jpeg and bmp files.
以下代码允许上传gif,png,jpg,jpeg和bmp文件。
var extension = $('#your_file_id').val().split('.').pop().toLowerCase();
if($.inArray(extension, ['gif','png','jpg','jpeg','bmp']) == -1) {
alert('Sorry, invalid extension.');
return false;
}
#7
0
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 = '';
}
}
#8
0
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function(){
jQuery('.form-file input').each(function () {
$this = jQuery(this);
$this.on('change', function() {
var fsize = $this[0].files[0].size,
ftype = $this[0].files[0].type,
fname = $this[0].files[0].name,
fextension = fname.substring(fname.lastIndexOf('.')+1);
validExtensions = ["jpg","pdf","jpeg","gif","png","doc","docx","xls","xlsx","ppt","pptx","txt","zip","rar","gzip"];
if ($.inArray(fextension, validExtensions) == -1){
alert("This type of files are not allowed!");
this.value = "";
return false;
}else{
if(fsize > 3145728){/*1048576-1MB(You can change the size as you want)*/
alert("File size too large! Please upload less than 3MB");
this.value = "";
return false;
}
return true;
}
});
});
});
</script>
</head>
<body>
<form>
<div class="form-file">
<label for="file-upload" class="from-label">File Upload</label>
<input class="form-control" name="file-upload" id="file-upload" type="file">
</div>
</form>
</body>
</html>
Here is complete answer for checking file size and file extension. This used default file input field and jQuery library. Working example : https://jsfiddle.net/9pfjq6zr/2/
这是检查文件大小和文件扩展名的完整答案。这使用了默认文件输入字段和jQuery库。工作示例:https://jsfiddle.net/9pfjq6zr/2/