如何将FileUpload的多个ID传递给javascript函数 [英] how to pass multiple id of FileUpload to javascript function
问题描述
I am working on asp.net web application in which i have 6 FileUpload Controls. for one file upload i have created a javascript method to check file extension and file size. but how to pass id of FileUpload upload as a parameter so that only in only one method i can validate all FileUpload my code of javacript is
var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "htm", "html", "rar", "zip", "txt", "pdf"];
function CheckExtension() {
/*global document: false */
var file = document.getElementById("<%=txtTenderDoc.ClientID%>");
var path = file.value;
var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
var isValidFile = false;
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (!isValidFile) {
alert("Invalid File. Unknown Extension Of Tender Doc" + "Valid extensions are:\n\n" + validFilesTypes.join(", "));
}
return isValidFile;
}
function validateFileSize() {
/*global document: false */
var file = document.getElementById("<%=txtTenderDoc.ClientID%>");
var fileSize = file.files[0].size;
var isValidFile = false;
if (fileSize !== 0 && fileSize <= 25214400) {
isValidFile = true;
}
if (!isValidFile) {
alert("File Size Should be Greater than 0 and less than 25 mb");
}
return isValidFile;
}
我已经在aspx页面中使用过此功能
and i have used this in aspx page
<asp:FileUpload ID="txtTenderDoc" onchange="var result= CheckExtension();validateFileSize(); return result"
runat="server"></asp:FileUpload>
如您所见,我必须以此创建 6方法,以检查文件大小和文件扩展名如何仅用一种方法来实现....
已更新
看到此
我已将JavaScript更改为
as you can see i have to create 6 method by this to check file size and file extension how to do that in only one method....
Updated
after seeing this
i have changed my javascript to
var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "htm", "html", "rar", "zip", "txt", "pdf"];
function CheckExtension(Id)
{
/*global document: false */
var file = document.getElementById(Id);
debugger;
var path = file.value;
var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
var isValidFile = false;
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (!isValidFile) {
alert("Invalid File. Unknown Extension Of Tender Doc" + "Valid extensions are:\n\n" + validFilesTypes.join(", "));
}
return isValidFile;
}
function validateFileSize(Id) {
/*global document: false */
var file = document.getElementById(Id);
var fileSize = file.files[0].size;
var isValidFile = false;
if (fileSize !== 0 && fileSize <= 25214400) {
isValidFile = true;
}
if (!isValidFile) {
alert("File Size Should be Greater than 0 and less than 25 mb");
}
return isValidFile;
}
并这样称呼
and calling it like this
<asp:FileUpload ID="flTenderDoc" onchange="var result=CheckExtension('flTenderDoc');validateFileSize('flTenderDoc'); return result"
runat="server"></asp:FileUpload>
但是它不起作用......
谁能帮忙这是我的第一个javascript方法:)
but it is not working ......
can anyone help this is my first javascript method :)
推荐答案
解决方案
始终遵循第二种方法.你错过了什么.让我纠正.
Solution
Always follow the second method. You have missed something. let me correct.
var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "htm", "html", "rar", "zip", "txt", "pdf"];
function CheckExtension(file) {
/*global document: false */
//var file = document.getElementById(this.ClientID);
var path = file.value;
var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
var isValidFile = false;
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (!isValidFile) {
alert("Invalid File. Unknown Extension Of Tender Doc" + "Valid extensions are:\n\n" + validFilesTypes.join(", "));
}
return isValidFile;
}
function validateFileSize(file) {
/*global document: false */
//var file = document.getElementById(this.ClientID);
var fileSize = file.files[0].size;
var isValidFile = false;
if (fileSize !== 0 && fileSize <= 25214400) {
isValidFile = true;
}
if (!isValidFile) {
alert("File Size Should be Greater than 0 and less than 25 mb");
}
return isValidFile;
}
标记为我已在函数内部提供了参数并注释了该行...
Mark that I have provided the arguments inside functions and commented out the line...
//var file = document.getElementById(this.ClientID);
因为现在您将在file
上作为参数接收的所有内容.
注意
我经过了最后的测试,一切正常.
Because you will now do everything on file
which you received as argument.
Note
I have tested at my end, it is working fine.
这是我为您的解决方案所做的事情,其余的您可以自行完成
it is what i did for your solution rest of you can do by your self
<head runat="server">
<title></title>
<script src="jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
(文档).ready(功能(){ var validFilesTypes = [" , " ," png", jpg"," ," ," , xls", xlsx ", htm", html", rar"," , txt", pdf"];
(document).ready(function () { var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "htm", "html", "rar", "zip", "txt", "pdf"];
这篇关于如何将FileUpload的多个ID传递给javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!