使用FileUpload实现多个文件同时上传

        最近公司做东西要求上传的地方有点多,所以现在想写点关于上传的东西。
        最近在网上看到一个朋友的视频教程,使用FileUpload实现多个文件同时上传的例子,感觉他的方法比较简单,所以就想写下来供需要的朋友使用。下面就是主要的界面,大家可以参考一下。下面我们来解释他的实现方法:

首先我们看一下界面,开始界面上有个Panel1,里面有个FileUpload控件,然后在Panel1外面有个TextBox控件,一个Button按钮,用来实现增加FileUpload控件的,还有一个Button按钮是用来实现上传功能的。看一些界面源文件:

<asp:Panel ID="Panel1" runat="server" Height="50px" Width="302px">
            
<asp:FileUpload ID="ful" runat="server" />
            
<input id="File1" runat="server" type="file" /></asp:Panel>
    
    
</div>
        
<asp:TextBox ID="textSum" runat="server" Width="63px"></asp:TextBox>
        
<asp:Button ID="AddBtn" runat="server" OnClick="AddBtn_Click" Text="增加" />
        
<asp:Button ID="FileUploadBtn" runat="server" OnClick="FileUploadBtn_Click" Text="全部上传" />
界面设计好以后看一下后台的代码怎么写的,其实后台的代码是很简单的,先看看怎么生成多的FileUpload控件的。
protected void AddBtn_Click(object sender, EventArgs e)
    
{
        System.Web.UI.WebControls.FileUpload fu;
        
int sum =Convert.ToInt32(textSum.Text);
        
for (int i = 0; i < sum; i++)
        
{
            fu 
= new System.Web.UI.WebControls.FileUpload();
            fu.ID 
= "fu_" + i.ToString();
            
//fu.file
            Panel1.Controls.Add(fu);
        }

    }
控件生成以后就要实现上传的功能了,上传的功能也是很简单的,大家看一下:
protected void FileUploadBtn_Click(object sender, EventArgs e)
    
{
        
string f_name, f_size, f_type;
        HttpFileCollection hfc 
= Request.Files;
        
for (int i = 0; i < hfc.Count; i++)
        
{
            HttpPostedFile hpf 
= hfc[i];
            f_name 
= hpf.FileName;
            f_size 
= hpf.ContentLength.ToString();
            f_type 
= hpf.ContentType;
            
string filename = System.IO.Path.GetFileName(f_name);
            hpf.SaveAs(
@"E:\fileupload\"+filename);
            Response.Write(
"<br>" + f_name + "<br>" + f_size + "<br>" + f_type);
        }

    }

好了,这样就可以实现多个文件同时上传了。


注意:这里因为是作为例子来弄的,对于一些界面的控制和一些对用户输入的控制都没有写出来。如果具体使用的话自己根据具体情况添加一些控件即可。
posted on 2007-05-15 16:49  Edwin dong  阅读(3644)  评论(6编辑  收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>