Upfile FSO
2024-07-21 02:24:41
供稿:网友
<%@ page language = "c#" debug = "true" %>
<%@ import namespace = "system.io" %>
<script language = "c#" runat = "server">
public void uploadfile(object sender , eventargs e) {
//检查上传文件不为空
if(inputfile.postedfile.contentlength > 0) {
//设定上传文件的保存路径
string strsavedir = "./upload/";
string strname = inputfile.postedfile.filename;
//取得文件名(抱括路径)里最后一个"."的索引
int intext = strname.lastindexof(".");
//取得文件扩展名
string strext = strname.substring(intext);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
/*
datetime datnow = datetime.now;
string strnewname = datnow.dayofyear.tostring() + inputfile.postedfile.contentlength.tostring() + strext; */
//取得文件名(包括路径)里最后一个"/"的索引
int intpath = strname.lastindexof("//");
//取得文件名(不包括路径)
string strnewname = strname.substring(intpath);
//保存文件到你所要的目录,这里是iis根目录下的upload目录.你可以改变.
//注意: 我这里用server.mappath()取当前文件的绝对目录.在asp.net里"/"必须用"//"代替
inputfile.postedfile.saveas(server.mappath(strsavedir + strnewname));
//得到这个文件的相关属性:文件名,文件类型,文件大小
labelupresult.text = "上传成功!";
labelfilename.text = "文件源:" + strname;
labelfileext.text = "文件类型:" + inputfile.postedfile.contenttype + "( " + strext + " )";
labelfilesize.text = "文件大小:" + (inputfile.postedfile.contentlength / 1024).tostring() + " k byte(s)";
} else {
labelupresult.text = "请选择你要上传的文件!";
labelfilename.text = "";
labelfileext.text = "";
labelfilesize.text = "";
}
}
</script>
<html>
<head>
<title>上传文件</title>
</head>
<body>
<div align="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#eeeeee" id="autonumber1">
<form id="formfile" method="post" action="" enctype="multipart/form-data" runat="server">
<tr>
<td align="center">
<input type="file" id="inputfile" name="inputfile" runat="server" size="64">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="upload" onserverclick="uploadfile" id="button1" name="button1" runat="server">
</td>
</tr>
<tr>
<td align="center">
<asp:label id="labelupresult" runat="server" text="" font-bold="true" forecolor="#ff0000"/><br>
<asp:label id="labelfilename" runat="server" text="" font-bold="true" forecolor="#ff0000"/><br>
<asp:label id="labelfileext" runat="server" text="" font-bold="true" forecolor="#ff0000"/><br>
<asp:label id="labelfilesize" runat="server" text="" font-bold="true" forecolor="#ff0000"/><br>
</td>
</tr>
</form>
</table>
</div>
</body>
</html>