我正在處理ASP.NET中的一個項目,其中我想使用JavaScript獲取文件大小。其實,我得到了解決方案,這就像下面這樣在JavaScript中不使用FileSystemObject獲取文件大小
<script language="javascript">
var Fo =new ActiveXObject("Scripting.FileSystemObject");
var fileName=new String();
function Trim(input)
{
var lre = /^\s*/;
var rre = /\s*$/;
input = input.replace(lre, "");
input = input.replace(rre, "");
return input;
}
function getSize(filePath)
{
try
{
var thefile = Fo.getFile(filePath);
var size = thefile.size;
return size;
}
catch(err){}
}
function getType(filePath)
{
try
{
var thefile = Fo.getFile(filePath);
var fileType = thefile.type;
return fileType;
}
catch(err){}
}
function ShowErrorPnl(tblPnl)
{
document.getElementById(tblPnl).style.visibility='visible';
document.getElementById(tblPnl).style.backgroundColor='Yellow';
document.getElementById(tblPnl).style.borderColor='Silver';
}
function UploadFile_CheckType(fileUploadCtrl,messageCtrl,hFieldCtrl,tblPnl)
{
try
{
var file = document.getElementById(fileUploadCtrl);
var fileName=file.value;
document.getElementById(messageCtrl).innerText='';
document.getElementById(tblPnl).style.visibility='hidden';
document.getElementById(hFieldCtrl).value=''; // File can be uploaded.
//Checking for file browsed or not
if((getSize(fileName)/1024)>500)
{
document.getElementById(messageCtrl).innerText=' File size is exceeding 500K';
document.getElementById(hFieldCtrl).value='0'; // File cannot upload.
ShowErrorPnl(tblPnl);
file.focus();
return false;
}
if(getType(fileName)!='Microsoft Office Word 97 - 2003 Document' &&
getType(fileName)!='Adobe Acrobat Document')
{
document.getElementById(messageCtrl).innerText=
(document.getElementById(messageCtrl).innerText=='' ?
' Only Doc and pdf file can be upload!!!' :
document.getElementById(messageCtrl).innerText +
'\n'+' Only Doc and pdf file can be upload!!!');
ShowErrorPnl(tblPnl);
document.getElementById(hFieldCtrl).value='0'; // File cannot upload.
file.focus();
return false;
}
}
catch(err){}
}
</script>
它是完美的工作,除非在Internet Explorer屬性中進行一些設置。
在Internet屬性 - >選擇安全選項卡 - >本地互聯網 - >點擊自定義級別按鈕 - >初始化和腳本的ActiveX控件未標記爲可安全執行腳本 - >讓它啓用
保存設置並執行程序。
這是工作,但我希望得到的文件大小不 使用Web頁面上var Fo =new ActiveXObject("Scripting.FileSystemObject");