多文件上传的例子。前台
<%@   Page   language= "c# "   Codebehind= "upMoreFile.aspx.cs "   AutoEventWireup= "false "   Inherits= "CommonFunction.upMoreFile "   %>
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN "   >
<HTML>
<HEAD>
<title> upMoreFile </title>
<meta   content= "Microsoft   Visual   Studio   7.0 "   name= "GENERATOR ">
<meta   content= "C# "   name= "CODE_LANGUAGE ">
<meta   content= "JavaScript "   name= "vs_defaultClientScript ">
<meta   content= "http://schemas.microsoft.com/intellisense/ie5 "   name= "vs_targetSchema ">
<script   language= "JavaScript ">
        function   addFileControl()
        {
        var   str   =   ' <INPUT   type= "file "   NAME= "File "> '
        document.getElementById( 'FileCollection ').insertAdjacentHTML( "beforeEnd ",str)
        }
</script>
</HEAD>
<body   MS_POSITIONING= "GridLayout ">
<form   id= "upMoreFile "   method= "post "   encType= "multipart/form-data "   runat= "server ">
<asp:label   id= "Title "   Runat= "server "> </asp:label>
<P   id= "FileCollection "> <INPUT   type= "file "   name= "File ">
</P>
<P> <input   onclick= "addFileControl() "   type= "button "   value= "增加(File) ">
<asp:button   id= "Upload "   Runat= "server "   Text= "上传 "   Width= "56px "> </asp:button> <input   style= "WIDTH:   56px;   HEIGHT:   24px "   onclick= "this.form.reset() "   type= "button "   value= "重置 ">
</P>
<P   align= "center "> <asp:label   id= "strStatus "   runat= "server "   BorderColor= "White "   BorderStyle= "None "   Width= "500px "
Font-Size= "9pt "   Font-Bold= "True "   Font-Names= "宋体 "> </asp:label> </P>
</form>
</body>
</HTML>
后台:
using   System;
using   System.Collections;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Web;
using   System.Web.SessionState;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.HtmlControls;namespace   CommonFunction
{
///   <summary>
///   upMoreFile   的摘要说明。
///   </summary>
public   class   upMoreFile   :   System.Web.UI.Page
{
protected   System.Web.UI.WebControls.Button   Upload;
protected   System.Web.UI.WebControls.Label   Title;
protected   System.Web.UI.WebControls.Label   strStatus;private   void   Page_Load(object   sender,   System.EventArgs   e)
{
Title.Text   =   " <h3> 多文件上传 </h3> ";
Upload.Text   =   "开始上传 ";
}#region   Web   Form   Designer   generated   code
override   protected   void   OnInit(EventArgs   e)
{
//
//   CODEGEN:该调用是   ASP.NET   Web   窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{        
this.Upload.Click   +=   new   System.EventHandler(this.Upload_Click);
this.Load   +=   new   System.EventHandler(this.Page_Load);}
#endregionprivate   void   Upload_Click(object   sender,   System.EventArgs   e)
{
upMorefile();
}private   bool   upMorefile()
{
//遍历File表单元素
System.Web.HttpFileCollection   files   =   System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder   strMsg   =   new   System.Text.StringBuilder( "上传的文件信息分别为: <hr   color=red> ");
int   fileCount;
int   filecount   =   files.Count;
try
{
for(fileCount   =   0;fileCount <files.Count;fileCount++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile   postedFile   =   files[fileCount];
string   fileName,   fileExtension;
//取得上传得文件名
fileName   =   System.IO.Path.GetFileName(postedFile.FileName);
if(fileName   !=   String.Empty)
{
//取得文件的扩展名
fileExtension   =   System.IO.Path.GetExtension(fileName);
//上传的文件信息
strMsg.Append( "上传的文件类型: "   +   postedFile.ContentType.ToString()   +   " <br> ");
strMsg.Append( "客户端文件地址: "   +   postedFile.FileName   +   " <br> ");
strMsg.Append( "上传文件的文件名: "   +   fileName   +   " <br> ");
strMsg.Append( "上传文件的扩展名: "   +   fileExtension   +   " <br> <hr   color=red> ");
//保存到指定的文件夹
postedFile.SaveAs(Server.MapPath( "upedFile/ ")   +   fileName);
}
}
strStatus.Text   =   strMsg.ToString();
return   true;
}
catch(System.Exception   error)
{
strStatus.Text   =   error.Message;
return   false;}
}
}

解决方案 »

  1.   


    FTP上传
    private static void UploadFile(string localFile) 

    FileInfo fi = new FileInfo(localFile); 
    FileStream fs = fi.OpenRead(); 
    long length = fs.Length; 
    FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name); 
    req.Credentials = new NetworkCredential(FtpUid, FtpPwd); 
    req.Method = WebRequestMethods.Ftp.UploadFile; 
    req.UseBinary = true; 
    req.ContentLength = length; 
    req.Timeout = 10 * 1000; 
    try 

    Stream stream = req.GetRequestStream(); 
    int BufferLength = 2048; 
    byte[] b = new byte[BufferLength]; 
    int i; 
    while ((i = fs.Read(b, 0, BufferLength)) > 0) 

    stream.Write(b, 0, i); 

    stream.Close(); 
    stream.Dispose();  } 
    catch (Exception ex) 

    Console.WriteLine(ex.ToString()); 
    } } 
      

  2.   

    自定义固定格式多文件批量上传using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Index : System.Web.UI.Page {     static public ArrayList hif = new ArrayList(); //10001 保存文件列表     public int filesUploaded = 0; // 10002上传文件的数量     protected void Page_Load(object sender, EventArgs e)     {     }     //10003-----------------添加列表     protected void AddFile_Click(object sender, EventArgs e)     {         if (Page.IsPostBack==true)  //这个很重要哦.呵呵.按键在第一次PAGE加载后才起作用         {             if (FindFile.HasFile)  //判断浏览文本框中有没有文件内容             {                 hif.Add(FindFile);//把文件完整路径添加到数组中去                 FileList.Items.Add( System.IO.Path.GetFileName(FindFile.PostedFile.FileName)); //获取一个待上传的文件名             }             else             {                 TipInfo.Text = "<font color=red>错误 - 必须要有上传的文件.</font>";                         }         }         else         { }     }     //10004从listbox中删除指定的文件     protected void DelFile_Click(object sender, EventArgs e)     {         if (FileList.SelectedIndex == -1)  //if the Listbox's item is not selected         {             TipInfo.Text = "<font color=red>错误 - 必须指定要删除的文件.</font>";             return;         }         else if (FileList.Items.Count != 0)         {             hif.RemoveAt(FileList.SelectedIndex); //Remove the item which is selected from the ArryList             FileList.Items.Remove(FileList.SelectedItem.Text); //Remove the item which is selected in the listbox.             TipInfo.Text = "";         }              }     /// <summary>     ///10005 循环上传listbox中的文件到指定的文件夹下     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     protected void Upload_Click(object sender, EventArgs e)     {         string LocalPath = Server.MapPath("UploadFiles/"); // 上传路径          string status = "";  // 上传成功后显示的文件列表                if ((FileList.Items.Count == 0) && (filesUploaded == 0)) //judge the listbox'item is nothing         {             TipInfo.Text = "<font color=red>错误 - 必须指定要上传的图片文件.</font>";             return;         }         else         {             foreach (System.Web.UI.WebControls.FileUpload HIF in hif)                     {                         try                         {                             Boolean fileOK = false;                             String fileExtension = System.IO.Path.GetExtension(HIF.FileName).ToLower();                             String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };                             for (int i = 0; i < allowedExtensions.Length; i++)                             {                                 if (fileExtension == allowedExtensions)                                 {                                     fileOK = true;                                 }                             }                                                       if (fileOK)                             {                                 string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);                                 HIF.PostedFile.SaveAs(LocalPath + fn);                                 filesUploaded++;                                 status += fn + "<br>";                             }                             else                             {                                 TipInfo.Text = "<font color=red>对不起.你上传的图片格式不对!</font>";                                 FileList.Items.Clear();                             }                         }                         catch (Exception err)                         {                             TipInfo.Text = "上传错误 " + LocalPath                             + "<br>" + err.ToString();                         }                     }                     if (filesUploaded == hif.Count)                     {                         TipInfo.Text = "共上传了 " + filesUploaded + " 个图片文件。 <br>" + status;                     }                     hif.Clear();                     FileList.Items.Clear();                                                 }     } }  
      

  3.   

    如果文件名是有规则的,用js动态创建fileupload,在服务器端获取PostedFiles数组,挨个上传
      

  4.   

    如果文件名是有规则的,用js动态创建fileupload,在服务器端获取PostedFiles数组,挨个上传
     这个方法不错~建议采纳如果用ftp的话?
    private static void UploadFile(string localFile) 

        FileInfo fi = new FileInfo(localFile); 
        FileStream fs = fi.OpenRead(); 
        long length = fs.Length; 
        FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name); 
        req.Credentials = new NetworkCredential(FtpUid, FtpPwd); 
        req.Method = WebRequestMethods.Ftp.UploadFile; 
        req.UseBinary = true; 
        req.ContentLength = length; 
        req.Timeout = 10 * 1000; 
        try 
        { 
            Stream stream = req.GetRequestStream(); 
            int BufferLength = 2048; 
            byte[] b = new byte[BufferLength]; 
            int i; 
            while ((i = fs.Read(b, 0, BufferLength)) > 0) 
            { 
                stream.Write(b, 0, i); 
            } 
            stream.Close(); 
            stream.Dispose();     } 
        catch (Exception ex) 
        { 
            Console.WriteLine(ex.ToString()); 
        } } 
      

  5.   

    如果文件名是有规则的,用js动态创建fileupload,在服务器端获取PostedFiles数组,挨个上传想法不错,不知道有具体应该怎么弄~