我想在Javascript中进行验证,文件名应该是特定类型的,应该上传一个例子.
我有一个fileupload控件,它具有上传multipe文件的功能.
这是html.
<asp:FileUpload runat="server" ID="flufileUpload" AllowMultiple="true" onchange="return ValidateFileType(this);" />
喜欢:-
1 – > 120.jpg
2 – > 150.jpg
3 – > 180.jpg
4 – > 210.jpg
5 – > 240.jpg
function ValidateFileType(controlName) {
try {
var uploadControl = controlName;
var isCValidFile = false;
for (var i = 0; i < uploadControl.files.length; i++) {
isCValidFile = false;
var path = uploadControl.files[i].name;
var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
if (path != "") {
for (var j = 0; j < ValidImageFileExtension.length; j++) {
if (ext == ValidImageFileExtension[j]) {
isCValidFile = true;
break;
}
}
if (!isCValidFile) {
jAlert("Invalid File. Please upload a File with extension " + ValidImageFileExtension.join(", "), "information");
uploadControl.value = '';
isCValidFile = false;
break;
}
else if (uploadControl.files[i].size > 15728640) {
jAlert("Please check file size should be less than 15 MB.", "information");
uploadControl.value = '';
isCValidFile = false;
break;
}
}
else {
jAlert("Please select File", "information");
isCValidFile = false;
break;
}
}
return isCValidFile;
} catch (e) {
isCValidFile = false;
jAlert("Error occurred on validate file.", "Error");
return isCValidFile;
}
}
解决方法:
您可以使用有效文件名对象检查文件名,还需要知道哪个选项卡处于活动状态.你可以使用以下代码
var Tabtype = document.getElementById('hdnType').value;
if (Tabtype == "Panaromic") {
var validFileName = {
"120.jpg": 1, "150.jpg": 1, "180.jpg": 1, "210.jpg": 1, "240.jpg": 1,
}
}
if (Tabtype == "Satellite") {
var validFileName = {
"55.jpg": 1, "74.jpg": 1, "83.jpg": 1, "935.jpg": 1,
}
}
if (Tabtype == "SitePlot") {
var validFileName = 0;
}
if (path != "") {
if(validFileName && !isFileNameValid(path)){
jAlert("Invalid File. Please upload a File with extension " +
ValidImageFileExtension.join(", "), "information");
uploadControl.value = '';
isCValidFile = false;
break;
}
...