一次上传多个文件

本文介绍了一个使用 ASP.NET 实现的多文件上传功能示例,包括客户端界面设计和服务器端处理逻辑。该示例支持上传多种类型的文件,并在服务器端进行格式和大小检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一次上传多个文件示例代码:

cs部分

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. public partial class fileupload : System.Web.UI.Page
  13.   { 
  14.    protected void Page_Load(object sender, EventArgs e) 
  15.    { 
  16.    
  17.    } 
  18.    protected void btnUpload_Click(object sender, EventArgs e) 
  19.   { 
  20.    lblMessage.Text = ""
  21.    lblMessage.Visible = false
  22.    System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; 
  23.    System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
  24.    string[] rd = Request.Form[1].Split(',');//文件描述 
  25.    string albumid=ddlAlbum.SelectedValue.Trim(); 
  26.    int ifile; 
  27.    for (ifile = 0; ifile < files.Count; ifile++) 
  28.    { 
  29.      if (files[ifile].FileName.Length > 0) 
  30.      { 
  31.         System.Web.HttpPostedFile postedfile = files[ifile]; 
  32.     if (postedfile.ContentLength / 1024 > 1024)//单个文件不能大于1024k 
  33.     { 
  34.     strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于1024k<br>"); 
  35.     break
  36.     } 
  37.    string fex = Path.GetExtension(postedfile.FileName); 
  38.     if (fex != ".jpg" && fex != ".rar" && fex != ".gif" && fex != ".doc"
  39.    { 
  40.     strmsg.Append(Path.GetFileName(postedfile.FileName) + "---格式不对<br>"); 
  41.     break
  42.     } 
  43.     } 
  44.    } 
  45.    if (strmsg.Length <= 0)
  46.    { 
  47.    //以下为创建文件目录 
  48.    string dirname = "pic00" + ddlAlbum.SelectedValue.Trim();
  49.    string dirpath = Server.MapPath("./") + "upload"
  50.    dirpath = dirpath + @"/" + dirname;
  51.    if (Directory.Exists(dirpath) == false
  52.    { 
  53.    Directory.CreateDirectory(dirpath); 
  54.    } 
  55.    Random ro = new Random(); 
  56.    int name = 1; 
  57.    for (int i = 0; i < files.Count; i++) 
  58.    { 
  59.    System.Web.HttpPostedFile myFile = files[i]; 
  60.    string FileName = ""
  61.    string FileExtention = ""
  62.    FileName = System.IO.Path.GetFileName(myFile.FileName); 
  63.    string stro=ro.Next(100,100000000).ToString()+name.ToString();//产生一个随机数用于新命名的文件
  64.    string NewName =DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+stro; 
  65.    if (FileName.Length > 0)//有文件才执行上传操作 
  66.    { 
  67.    FileExtention = System.IO.Path.GetExtension(myFile.FileName);
  68.    string ppath = dirpath + @"/" + FileName + FileExtention; 
  69.    myFile.SaveAs(ppath); 
  70.    string FJname = FileName; 
  71.    
  72.    }
  73.    Response.Write(rd[i].ToString());
  74.        //写入数据库
  75.    //AddPicture(PicPath, rd[i], albumid); 
  76.    } 
  77.    } 
  78.    else 
  79.    { 
  80.    lblMessage.Text = strmsg.ToString(); 
  81.    lblMessage.Visible = true
  82.    } 
  83.    } 
  84.     } 
  85. html部分
    1. <div style="font-size:9pt;"
    2.        <br />
    3.        <table >
    4.            <tr valign="top">
    5.                <td >
    6.                    请选择文件:</td>
    7.                <td ><div id="MyFile">
    8.    <input id="File1" type="file" name="File" runat="server" style="width: 250px"  onchange="uploadSelect($(this));"/> 描述:<input name="text" type="text" style="width: 150px" maxlength="20" /><br /></div></td>
    9.                <td style="width: 100px">
    10.    <input onclick="addFile()" type="button" value="增加文件"/></td>
    11.            </tr>
    12.            <tr>
    13.                <td >
    14.                    指定文件夹:</td>
    15.                <td >
    16.    <asp:DropDownList ID="ddlAlbum" runat="server" Width="98px"
    17.    </asp:DropDownList>
    18.    <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" /> </td>
    19.                <td >
    20.    <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></td>
    21.            </tr>
    22.        </table>
    23.    </div>
  86. js部分
    1. <script type="text/javascript">
    2.    var i=1 
    3.    function addFile() 
    4.    { 
    5.    
    6.    if (i<6) 
    7.    {var str = ' <input type="file" name="File" runat="server" style="width: 250px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" /><br/>' 
    8.    document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str) 
    9.    
    10.    } 
    11.    else 
    12.    { 
    13.    alert("您一次最多只能上传6个文件!"
    14.    } 
    15.    i++ 
    16.    } 
    17.         var uploadSelect = function(el){
    18.             el.fadeOut("show");     
    19.             parent.uploading(document.getElementById("<%=File1.ClientID %>").value);
    20.             $("#<%=frmUpload.ClientID %>").submit();
    21.         };  
    22.    </script> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值