使用asp.net 制作一个可以存放client 端档案的网页相当容易,因为asp.net 里就有提供我们将client 端档案传至server 端的对象,这个对象就是htmlinputfile 对象。htmlinputfile 对象必须存在窗体中,而且窗体<form> 标注中必须加入设定enctype="multipart/form-data" 属性才可使用。htmlinputfile 对象的语法如下所示:
<input type="file" id="被程序所控制的名称" runat="server">
当一个档案传送到server 端后,接收和处理的是htmlinputfile 对象的postedfile 属性。postedfile 属性的型态是httppostedfile 对象类别,其常用属性如下表所示:

其常用方法如下表所示:

基本档案上传
下列范例使用htmlinputfile 对象的基本用法,分别印出档案的名称、大小、类型:
<html>
<form enctype="multipart/form-data" runat="server">
<input type="file" id="upload" runat="server">
<asp:button id="button1" text="ok" onclick="button1_click"
runat="server" /><br>
<asp:label id="label1" runat="server" />
</form>
<script language="vb" runat="server">
sub button1_click(sender as object,e as eventargs)
dim strcontent as string
if upload.postedfile.contentlength<0 then
label1.text="上传失败"
else
strcontent="文件名称:" & upload.postedfile.filename
strcontent=strcontent & "<br>档案大小:" & _
cstr(upload.postedfile.contentlength) & "bytes"
strcontent=strcontent & "<br>档案类型:" &
upload.postedfile.contenttype
end if
label1.text=strcontent
end sub
</script>
</html>

存入磁盘
上传档案后若要写入磁盘中也相当容易,只要使用postedfile 的saveas 方法即可。档案写入时若直接将postedfile.filename 属性来当文件名写入会有问题,因为filename 属性包含有档案的路径名称,所以必须先将档案的路径分开。下列范例使用split 函数来取得文件名称,并将档案存放到网页所在的目录:
<html>
<form enctype="multipart/form-data" runat="server">
<input type="file" id="upload" runat="server">
<asp:button id="button1" text="ok" onclick="button1_click"
runat="server" /><br>
<asp:label id="label1" runat="server" />
</form>
<script language="vb" runat="server">
sub button1_click(sender as object,e as eventargs)
dim strcontent as string
if upload.postedfile.contentlength>0 then
strcontent="文件名称:" & upload.postedfile.filename
strcontent=strcontent & "<br>档案大小:" &
cstr(upload.postedfile.contentlength) & "bytes"
strcontent=strcontent & "<br>档案类型:" &
upload.postedfile.contenttype
dim temp() as string = split(upload.postedfile.filename, "\" )
dim name as string = temp(temp.length-1)
upload.postedfile.saveas(server.mappath(".") & "\" & name)
label1.text=strcontent
else
label1.text="上传失败"
end if
end sub
</script>
</html>
split 函数会将一个字符串以我们设定的条件为分隔,并将分段后的字符串存至一个字符串数组中;所以我们只要以"\" 为条件来分隔档案,在字符串数组中的最后一个元素即为文件名称。
多档上传
我们也可以一次上传多个档案。管理档案的对象是request 对象的files 属性,它的型别是httpfilecollection 集合对象类别,它的项目成员是httppostedfile 类别对象。httpfilecollection型别变量的常用属性如下表所示:

其常用方法如下表所示:

下列范例中我们放了两个htmlinputfile 对象,并上传两个档案:
<html>
<form enctype="multipart/form-data" runat="server">
<input type="file" id="upload1" runat="server"><br>
<input type="file" id="upload2" runat="server">
<asp:button id="button1" text="ok" onclick="button1_click"
runat="server"/><br>
<asp:label id="label1" runat="server"/>
</form>
<script language="vb" runat="server">
sub button1_click(sender as object,e as eventargs)
dim shti as short
dim strcontent as string
dim files as httpfilecollection
files=request.files
for shti=0 to files.count-1
if files.item(shti).contentlength>0 then
strcontent=strcontent & "第" & cstr(shti+1) & "个档案上传对象
<br>"
strcontent=strcontent & "文件名称:" & files.item(shti).filename
& "<br>"
strcontent=strcontent & "档案大小:" &
cstr(files.item(shti).contentlength)_
& "byte<br>"
strcontent=strcontent & "档案类型:" &
files.item(shti).contenttype & "<p>"
dim temp() as string = split( files.item(shti).filename, "\")
dim name as string = temp(temp.length-1)
files.item(shti).saveas(server.mappath(".") & "\" & name)
end if
next
label1.text=strcontent
end sub
</script>
</html>

上述范例中我们先定义一个httpfilecollection 类型变量files,并将request.files 属性的内容放到files 变量中。再使用for....next 循环将档案信息加入strcontent 变量中,再以split 函数将档案路径和文件名称分开并且存到一数组,因数组的最后一个元素即为文件名称,因此个别取出后使用httppostedfile 对象的saveas 方法将档案写入磁盘。