上傳多個文件實例(net)

 《1》

<%@ Import Namespace="System.IO" %> <%@ page Language="C#" debug="true" %> <html>

<head> <title>文件上傳的實例</title>

<script language="C#" runat="server">     //This method is called when the "upload" button id pressed     public void UploadFile(object sender , EventArgs E) {         HttpFileCollection myFile  = HttpContext.Current.Request.Files;         for(int i=0; i<myFile.Count; i++) {             HttpPostedFile postedFile = myFile[i];                          if(postedFile.ContentLength != 0) {                 string fileName = Path.GetFileName(postedFile.FileName);                                  DateTime now = DateTime.Now;                 string nowtime=now.Year.ToString()+now.Month.ToString()+now.Day.ToString()+now.Hour.ToString()+now.Minute.ToString()+now.Second.ToString();                                  //獲得文件名擴展                 string fileExtension = System.IO.Path.GetExtension(fileName);                                  //保存文件到你所要的目錄,這裏是IIS根目錄下的upfiles目錄.你可以改變.                  //注意: 我這裏用Server.MapPath()取當前文件的絕對目錄.在asp.net裏"/"必須用"//"代替                  postedFile.SaveAs(Server.MapPath("~//upfiles//"+nowtime+fileName));                 //postedFile.SaveAs(Request.MapPath("upFiles/") +nowtime+fileName);                                  //得到這個文件的相關屬性:文件名,文件類型,文件大小                 fname.Text=postedFile.FileName;                 fenc.Text=postedFile.ContentType+"<br>"+fileExtension;                 fsize.Text=postedFile.ContentLength.ToString()+"<br>"+nowtime.ToString();             }         }     } </script>

</head>

<body>

<h3 align="center">文件上傳的實例 </h3> <div align="center">     <table border="1" cellspacing="2" cellpadding="2">         <form id="uploderform" method="post" action="FileUpload.aspx" enctype="multipart/form-data" runat="server">             <tr>                 <td><h5 align="center">選擇要上傳的文件:</h5></td>             </tr>             <tr>                 <td><input type="file" id="myFile" name="myFile"></td>             </tr>             <tr>                 <td><input type="file" id="File1" name="myFile"></td>             </tr>             <tr>                 <td><input type="file" id="File2" name="myFile"></td>             </tr>             <tr>                 <td><input type="button" value="上 傳" onserverclick="UploadFile" runat="server" id="Button1" name="Button1"></td>             </tr>         </form>     </table> </div> <br> <br> <div align="center">     <table border="1" cellspacing="2">         <tr>             <td><b>文件資料</b></td>             <td> </td>         </tr>         <tr>             <td>文件名</td>             <td><asp:Label ID="fname" Text="" runat="server" /></td>         </tr>         <tr>             <td>文件類型</td>             <td><asp:Label ID="fenc" runat="server" /></td>         </tr>         <tr>             <td>文件大小(in bytes)</td>             <td><asp:Label ID="fsize" runat="server" /></td>         </tr>     </table> </div>

</body>

</html>

 

<2>

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

<!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:Panel ID="Panel1" runat="server" Height="171px" Width="356px">             <asp:FileUpload ID="FileUpload1" runat="server" Width="273px" /><br />         </asp:Panel>         </div>         <asp:TextBox ID="TextBox1" runat="server" Width="93px"></asp:TextBox>&nbsp;<asp:Button             ID="Button1" runat="server" OnClick="Button1_Click" Text="增加" />         <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="上傳" />     </form> </body> </html>

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; using System.IO;

public partial class upFiles_Default2 : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {

    }     protected void Button1_Click(object sender, EventArgs e)     {         FileUpload fu;

        for (int i = 0; i < Convert.ToInt32(TextBox1.Text); i++)         {             fu = new FileUpload();             fu.ID = "fu" + i.ToString();             Panel1.Controls.Add(fu);         }     }     protected void Button2_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 = Path.GetFileName(hpf.FileName);             f_size = hpf.ContentLength.ToString();             f_type = hpf.ContentType;             hpf.SaveAs(Server.MapPath("~/up/")+f_name);             Response.Write(f_name+"<br>"+f_size+"<br>"+f_type+"<br>");         }

    } }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章