多个文件上传

来源:互联网 发布:mysql union all 排序 编辑:程序博客网 时间:2024/06/02 21:06

前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload .aspx.cs" Inherits="FileUpload_" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>上传多个文件</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:FileUpload ID="FileUpload2" runat="server" /><br />
        <asp:FileUpload ID="FileUpload3" runat="server" /><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>&nbsp;</div>
    </form>
</body>
</html>

后台程序

   protected void Button1_Click(object sender, EventArgs e)
    {
      string filepath = "C://Uploads";
   HttpFileCollection uploadedFiles = Request.Files;
   
   for (int i = 0; i < uploadedFiles.Count; i++)
   {   
      HttpPostedFile userPostedFile = uploadedFiles[i];
   
      try
      {   
         if (userPostedFile.ContentLength > 0 )
         {
        string str1 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
             DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() +
             userPostedFile.FileName.Substring(userPostedFile.FileName.LastIndexOf("//") + 1);//取文件名

              userPostedFile.SaveAs(filepath + "//" +str1); //保存文件
         }   
      }
      catch (Exception Ex)
      {   
         Label1.Text += "Error: " + Ex.Message;   
      }   
   }
    }
      

原创粉丝点击