function lastname (filepath) {
console.log('filepath : ' + filepath);
//为了避免转义反斜杠出问题,这里将对其进行转换
var re = /(
\\+)/g
;
var filename = filepath.replace(re,"#");
//对路径字符串进行剪切截取
var one=filename.split("#");
//获取数组中最后一个,即文件名
var two=one[one.length-1];
//再对文件名进行截取,以取得后缀名
var three=two.split(".");
//获取截取的最后一个字符串,即为后缀名
var last=three[three.length-1];
//添加需要判断的后缀名类型
var tp ="jpg,png,bmp,JPG,PNG,BMP";
//返回符合条件的后缀名在字符串中的位置
var rs=tp.indexOf(last);
//如果返回的结果大于或等于0,说明包含允许上传的文件类型
if(rs>=0){
return true;
}else{
return false;
}
}
$(function () {
$('#exampleInputFile1').fileupload({
add : function(e, data) {
console.log('data.files[0].name : ' + data.files[0].name);
var goUpload = true;
var uploadFile = data.files[0];
if (!lastname (uploadFile.name)) {
$("#mesg1").empty().html("上传文件类型不正确!");
goUpload = false;
}
if (uploadFile.size > 100000) { // 2mb
$("#mesg1").empty().html("上传文件大小100kb!");
goUpload = false;
}
if (goUpload == true) {
$("#mesg1").empty().html("上传成功!");
data.submit();
}
},
url: '<%=basePath%>admin/uploadFiles',
type:'POST',
done : function(e, result) {
var message = JSON.stringify(result.result.message);
var success = JSON.stringify(result.result.success);
var reg = new RegExp('"','g');
var cc = message.replace(reg,"");
if (success) {
var imgpath = "<%=basePath%>common/upload/images/" + cc;
$('#imgSrc1').attr("src", imgpath).show();
}
}
});
});
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">优惠券被兑换之前图片</label>
<input type="file" id="exampleInputFile1">
<p id="mesg1" class="help-block text-danger">图片大小在100KB以内,格式jpg,png,bmp</p>
</div>
<div class="form-group has-warning">
<label class="control-label" for="inputWarning"><i class="fa fa-bell-o"></i> 图片.</label>
<input type="text" class="form-control" name="couponUrl" id="couponUrl" />
<label for="couponUrl" class="text-danger"></label>
</div>
<div class="form-group">
<img id="imgSrc1" />
</div>
</div>
转载于:https://my.oschina.net/u/218308/blog/412771