我想在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;
}
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。