功能:上传指定文件,删除指定文件,操作前判断文件是否存在,判断文件大小(KB)
代码为javascript编写,完全是在客户端完成
<
html
>
< head >
< script language ="javascript" > ...
function changeNum(num,n)
...{
var dd=1;
var tempnum;
for(i=0;i<n;i++)
...{
dd*=10;
}
tempnum=num*dd;
tempnum=Math.round(tempnum);
return(tempnum/dd);
}
function getFileSize()
...{
var form_obj = document.form1;
var file_path = form_obj.upd_file.value.toString()
var fso;
var tf;
if (!form_obj.upd_file.value == "")
...{
fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(file_path))
...{
tf = fso.GetFile(file_path);
form_obj.file_size.value = changeNum(parseInt(tf.size)/1024,2);
}
else
...{
form_obj.file_size.value = "";
}
}
else
...{
form_obj.file_size.value = "";
}
}
function chk_upd()
...{
var form_obj = document.form1;
var file_path = form_obj.upd_file.value.toString();
var to_folder;
var fso;
var ft;
var to_folder = 'C:/';
if (form_obj.upd_file.value == "")
...{
alert("You must select a file!");
form_obj.upd_file.focus();
return false;
}
fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(file_path))
...{
alert("file doesn't exist!")
form_obj.upd_file.focus();
return false;
}
fso.CopyFile(file_path,to_folder);
alert("uploaded!");
}
function chk_del()
...{
var form_obj = document.form1;
var file_path = form_obj.upd_file.value.toString();
var to_folder;
var fso;
var ft;
var to_folder = 'C:/';
if (form_obj.upd_file.value == "")
...{
alert("You must select a file!");
form_obj.upd_file.focus();
return false;
}
fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(file_path))
...{
alert("file doesn't exist!")
form_obj.upd_file.focus();
return false;
}
fso.DeleteFile(file_path,true);
alert("deleted!");
}
</ script >
</ head >
< body >
< form method ="post" name ="form1" enctype ="multipart/form-data" >
< input type ="file" name ="upd_file" id ="upd_file" onchange ="getFileSize();" >
< button onclick ="chk_upd()" > Upload Now! </ button > < button onclick ="chk_del()" > Delete Now! </ button >< br >
size: < input type ="text" name ="file_size" id ="file_size" value ="" style ="border:0" ></ label >
</ form >
</ body >
</ html >
< head >
< script language ="javascript" > ...
function changeNum(num,n)
...{
var dd=1;
var tempnum;
for(i=0;i<n;i++)
...{
dd*=10;
}
tempnum=num*dd;
tempnum=Math.round(tempnum);
return(tempnum/dd);
}
function getFileSize()
...{
var form_obj = document.form1;
var file_path = form_obj.upd_file.value.toString()
var fso;
var tf;
if (!form_obj.upd_file.value == "")
...{
fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(file_path))
...{
tf = fso.GetFile(file_path);
form_obj.file_size.value = changeNum(parseInt(tf.size)/1024,2);
}
else
...{
form_obj.file_size.value = "";
}
}
else
...{
form_obj.file_size.value = "";
}
}
function chk_upd()
...{
var form_obj = document.form1;
var file_path = form_obj.upd_file.value.toString();
var to_folder;
var fso;
var ft;
var to_folder = 'C:/';
if (form_obj.upd_file.value == "")
...{
alert("You must select a file!");
form_obj.upd_file.focus();
return false;
}
fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(file_path))
...{
alert("file doesn't exist!")
form_obj.upd_file.focus();
return false;
}
fso.CopyFile(file_path,to_folder);
alert("uploaded!");
}
function chk_del()
...{
var form_obj = document.form1;
var file_path = form_obj.upd_file.value.toString();
var to_folder;
var fso;
var ft;
var to_folder = 'C:/';
if (form_obj.upd_file.value == "")
...{
alert("You must select a file!");
form_obj.upd_file.focus();
return false;
}
fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(file_path))
...{
alert("file doesn't exist!")
form_obj.upd_file.focus();
return false;
}
fso.DeleteFile(file_path,true);
alert("deleted!");
}
</ script >
</ head >
< body >
< form method ="post" name ="form1" enctype ="multipart/form-data" >
< input type ="file" name ="upd_file" id ="upd_file" onchange ="getFileSize();" >
< button onclick ="chk_upd()" > Upload Now! </ button > < button onclick ="chk_del()" > Delete Now! </ button >< br >
size: < input type ="text" name ="file_size" id ="file_size" value ="" style ="border:0" ></ label >
</ form >
</ body >
</ html >
几点说明:
(1)函数changeNum是用来进行四舍五入的,因为得到的文件大小默认单位是byte。
(2)file输入框采用onchange事件可以在选择完文件后就可以获得文件的大小。
(3)默认的上传目录设置到了c盘,可以根据自己的需要更改一下
代码还有不足之处,欢迎大家指正交流。