我使用uploadify上传Codeigniter文件.在上传文件之前,我只需要检查文件扩展名是否正确是否正确.我试过http://jquery.bassistance.de/和http://forum.jquery.com/
Bur没有得到适当的结果.谁能告诉我怎么能这样做?
提前致谢...
小智 40
如果你想在没有插件的情况下这样做,你可以使用以下内容.
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();
}
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<form id="your_form" method="post" enctype="multipart/form-data">
<input id="file_input" type="file" />
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
Gol*_*lda 35
您可以使用JQuery实现此目的
HTML
<input type="file" id="FilUploader" />
Run Code Online (Sandbox Code Playgroud)
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(', '));
}
});
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请单击此处
V15*_*M4Y 17
我要感谢发布答案的人,但他已删除了帖子.我们可以这样做.
$("#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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答,它真的有用......
小智 7
这是一个简单的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("");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
$("#file-upload").change(function () {
var validExtensions = ["jpg","pdf","jpeg","gif","png"]
var file = $(this).val().split('.').pop();
if (validExtensions.indexOf(file) == -1) {
alert("Only formats are allowed : "+validExtensions.join(', '));
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
125969 次 |
最近记录: |