1、
//我们得到文件后缀名后,根据后缀即可判断文件的类型(文件格式)。比如我们需要判断一个文件是否是图片格式,首先定义一个判断函数:
function isAssetTypeAnImage(ext) {
return [
'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].
indexOf(ext.toLowerCase()) !== -1;
}
//然后函数调用判断
//文件路径
var filePath = "file://upload/jb51.png";
//获取最后一个.的位置
var index= filePath.lastIndexOf(".");
//获取后缀
var ext = filePath.substr(index+1);
//判断是否是图片
console.log("该文件是否为图片:" + isAssetTypeAnImage(ext));
2、
// TODO:判断是否图片
function isAssetTypeAnImage(ext) {
return [
'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].
indexOf(ext.toLowerCase()) !== -1;
}
var imgShowWayArr = []
//判断是否是图片
$.each(list, function (index, item) {
// var $li = $('<li data-httpurl="' + item.httpUrl + '"><span>' + item.fileName + '</span><i class="iconfont icon-jian-yuankuang"></i></li>').appendTo($fileListUl);
// TODO:附件改为照片
//获取后缀
var ext = item.httpUrl.substr(item.httpUrl.lastIndexOf(".")+1);
var boo = isAssetTypeAnImage(ext)
imgShowWayArr.push(boo)
var $li =
imgShowWayArr[index]
?
$('<li style="height:100px;border-right:1px solid #A5ABB4" data-httpurl="' + item.httpUrl + '">' + '</span><a href="'+item.httpUrl+'" target="_blank"><img class="upload-img" src="'+item.httpUrl+'"/></a><i class="iconfont icon-jian-yuankuang"></i></li>').appendTo($fileListUl)
:
$('<li style="height:100px;border-right:1px solid #A5ABB4" data-httpurl="' + item.httpUrl + '">' + '</span><a href="'+item.httpUrl+'" target="_blank"><img class="upload-img" src="../../images/default-img.jpg"/></a><i class="iconfont icon-jian-yuankuang"></i></li>').appendTo($fileListUl)
// var $li = $('<li style="height:100px;border-right:1px solid #A5ABB4" data-httpurl="' + item.httpUrl + '">' + '</span><a href="'+item.httpUrl+'" target="_blank"><img class="upload-img" src="'+item.httpUrl+'"/></a><i class="iconfont icon-jian-yuankuang"></i></li>').appendTo($fileListUl);
$li.find('i').css('cursor', 'pointer').click(function (e) {
e.stopPropagation();
var httpUrl = $(this).parent().data('httpurl');
attachmentList = attachmentList.fakeFilter(function (item) {
return item.httpUrl !== httpUrl;
});
renderAttachmentList(attachmentList);
});
});