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 ** 閱讀(926) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          收藏夾

          C#學習

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 堆龙德庆县| 晋中市| 左贡县| 泰顺县| 崇仁县| 沐川县| 包头市| 谢通门县| 临邑县| 大关县| 康保县| 宣城市| 九江县| 绿春县| 台东市| 慈溪市| 辽中县| 霍山县| 南京市| 化德县| 大化| 赤城县| 荥经县| 武穴市| 锡林浩特市| 武乡县| 宾阳县| 武汉市| 西乌| 琼结县| 临泉县| 赞皇县| 汝城县| 深州市| 鱼台县| 梁平县| 中西区| 综艺| 滨海县| 大冶市| 陆河县|