关闭

多个文件上传

529人阅读 评论(0) 收藏 举报

前台页面

<%@ 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;   
      }   
   }
    }
      

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:828次
    • 积分:16
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档
    阅读排行
    评论排行