动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

 档案上传处理

作者来源: 
阅读 1774 人次 , 2006-3-29 4:32:00 


使用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 方法将档案写入磁盘。
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:E-Mail 传送
· 下一篇:程序的追踪及检视
· 随机函数生成密码的asp.net版本
· ASP.NET 程序设计-序
· .Net的Outofmemory异常及大内存使用
· 在ASP.NET中处理 datetime 的一些通用函数(vb)
· .net的reflection (1)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077