Hopes

          Start Here..

           

          asp.net,c#,同時上傳多個文件

           

          asp.net,c#,同時上傳多個文件

          2011-08-19 10:52 17人閱讀 評論(0) 收藏 舉報

          ASPX頁:

          <head runat="server">
              <title>多文件上傳</title>
              <script type="text/javascript" language="javascript">
                  function addForm()
                  {
                      var strForm ="<input type='file' size='50' name='File' />"
                      document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",strForm)
                  }
              </script>
          </head>
          <body>
              <form id="form1" runat="server" enctype="multipart/form-data">
              <p id="MyFile">
                    <input type="file" size="50" name="File" />
              </p>
              <p>
                    <input type="button" value="增加一個" onclick="addForm()" />
                    
                    <asp:Button Runat="server" Text="開始上傳" ID="UploadButton" 
                          onclick="UploadButton_Click">
                    </asp:Button>

                    
                    <input onclick="this.form.reset()" type="button" value="重 置" />
                    <br />
                    <asp:Label ID="Label1" runat="server"></asp:Label>
              </p>
              </form>
          </body>
          </html>

          cs頁:

          using System;
          using System.Collections;
          using System.Configuration;
          using System.Data;
          using System.Linq;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.HtmlControls;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Xml.Linq;

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

              }
              protected void UploadButton_Click(object sender, EventArgs e)
              {
                  this.SaveFiles();
              }
              private void SaveFiles()
              {
                  //遍歷表單元素
                  HttpFileCollection files = HttpContext.Current.Request.Files;

                  //狀態信息
                  string strout = "<br>上傳的文件分別是:<hr color=red><table style='width: 500px;'>";
                  strout += "<tr><td>文件類型</td><td>客戶端地址</td><td>上傳文件名</td><td>擴展名</td></tr>";
                  try
                  {
                      for (int iFile = 0; iFile < files.Count; iFile++)
                      {
                          //訪問單獨文件
                          HttpPostedFile postedFile = files[iFile];
                          string fileName, fileExtension;

                          fileName = System.IO.Path.GetFileName(postedFile.FileName);

                          if (fileName != "")
                          {
                              fileExtension = System.IO.Path.GetExtension(fileName);

                              strout += "<tr><td>" + postedFile.ContentType.ToString() + "</td>";
                              strout += "<td>" + postedFile.FileName + "</td>";
                              strout += "<td>" + fileName + "</td>";
                              strout += "<td>" + fileExtension + "</td></tr>";

                              postedFile.SaveAs(Server.MapPath("uploadFile/") + fileName);
                          }
                          else 
                          {
                              strout = "<br>請您選擇一個文件!!!";
                          }
                      }

                      Label1.Text = strout.ToString();

                  }
                  catch (Exception Ex)
                  {
                      Label1.Text = Ex.Message.ToString();
                  }
              }
          }

          posted on 2012-08-19 20:42 ** 閱讀(923) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          收藏夾

          C#學習

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 堆龙德庆县| 大埔区| 同德县| 溆浦县| 德令哈市| 湄潭县| 福州市| 建昌县| 千阳县| 四平市| 南充市| 东丽区| 辽宁省| 谷城县| 托克逊县| 康平县| 沭阳县| 施秉县| 罗定市| 儋州市| 重庆市| 汶川县| 尼木县| 广安市| 阿鲁科尔沁旗| 文山县| 吉隆县| 永川市| 泗阳县| 普格县| 晋江市| 安宁市| 天门市| 宝坻区| 漳平市| 铜梁县| 丽江市| 金秀| 乌拉特后旗| 曲周县| 江阴市|