收藏:ASP.NET 2.0 多文件上传小经验

本文介绍如何使用ASP.NET2.0实现多文件上传功能。通过设置form标签的enctype属性为multipart/form-data,确保服务器能正确接收多个文件。文章提供了具体的代码示例。
    想实现任意多个文件上传的功能,点击一次按钮可以添加一个文件上传框,以前在网络硬盘上看到过。JavaScript我知道怎么实现任意添加上传文件控件,问题是添加的是Html控件,我不懂怎么让服务器端可以获取文件。

    于是上google搜索“ASP.NET 多文件上传”,还真找到一篇文件,标题为《在ASP.NET中实现多文件上传》,文章里面是VB.NET实现的,功能和我要的一模一样,我主要是要看服务器端怎么获取客户端上传的文件,看了文中的代码,原来这么简单,System.Web.HttpContext.Current.Request.Files就包含客户端浏览器上传的文件了,我用C#写了一段简单的代码,原本以为应该可以了,结果出乎意料上传3个图片System.Web.HttpContext.Current.Request.Files返回的文件格式还是0个。

    不知道什么原因,看看代码,这么简单不可能些错啊,再看看google搜索结果里的另外几篇文章,发现我看的第一篇不是原做,作者的网站上原作的实例有两个版本,一个是VB.NET一个是C#的,现在我不用自己写了,复制原文的代码到本地,运行,果然可以啊,那我写的代码怎么不行?反复比对我的代码和文章中代码的区别,试了几个地方,最后发现和其他地方都没有关系,原因出在<form id="form1" runat="server" enctype="multipart/form-data"> 的enctype属性上,VS 2005建的页面里没有这个属性,而文章实例里有,我后来加上enctype="multipart/form-data"后System.Web.HttpContext.Current.Request.Files就能z正常获取文件个数了。

    大概是VS 2003建的页面默认有这个属性吧,否则这么重要的属性作者应该会在文章中提到的。

参考:
《在ASP.NET中实现多文件上传》

我做的试验代码:

ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo._Default" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
ExpandedBlockStart.gifContractedBlock.gif
<script language="JavaScript">dot.gif
InBlock.gif
function addFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
var str = '<INPUT type="file" size="50" NAME="File">'
InBlock.gif document.getElementById('MyFile').insertAdjacentHTML(
"beforeEnd",str)
ExpandedBlockEnd.gif}

None.gif
</script>
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>Untitled Page</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server" enctype="multipart/form-data">
None.gif           
<input type="button" value="增加(Add)" onclick="addFile()">
None.gif          
<input onclick="this.form.reset()" type="button" value="重置(ReSet)">
None.gif          
<asp:Button Runat="server" Text="上传" ID="Upload" OnClick="Upload_Click1" ></asp:Button>
None.gif    
<div id="MyFile">
None.gif         
<input type="file" name="File" />  
None.gif    
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  Demo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class _Default : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected void Upload_Click1(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            HttpFileCollection _files 
= System.Web.HttpContext.Current.Request.Files;
InBlock.gif
InBlock.gif            
for (int i = 0; i < _files.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _files[i].SaveAs(Server.MapPath(
"~/Files/" + _files[i].FileName));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
引用自 http://bg5sbk.cnblogs.com/archive/2006/06/11/mulitfileuploadtest.html

转载于:https://www.cnblogs.com/pjdu/archive/2006/06/12/424303.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值