Hopes

          Start Here..

           

          如何做: WEB多文件同時選擇后逐一上傳控件?

          問:
          如何做: WEB多文件同時選擇后逐一上傳控件?
          謝謝!~
          ______________________________________________________________________________________________
          答1:
          http://dotnet.aspx.cc/ShowDetail.aspx?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C
          ______________________________________________________________________________________________
          答2:
          TO :net_lover(孟子E章) :
          不用HTML的控件,因為它不能一次選擇多個文件,只能一個一個點擊,上傳多個文件時太麻煩了。我想一次可以框選擇多個文件,然后上傳,如何實現(xiàn)?
          ______________________________________________________________________________________________
          答3:
          給你例子看看:
          //upload.aspx
          <%@ Page language="c#" Codebehind="upload.aspx.cs" AutoEventWireup="false" Inherits="RiseGuide.MegaNet.admin.upload" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
          <HTML>
          <HEAD>
          <title>upload</title>
          <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
          <meta name="CODE_LANGUAGE" Content="C#">
          <meta name="vs_defaultClientScript" content="JavaScript">
          <LINK href="../cmdjs/wgnet_style.css" type="text/css" rel="stylesheet">
          <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
          <script id="clientEventHandlersJS" language="javascript">
          <!--
          var i;
          function File_onblur(e) {
          try{
          i=e.name.replace("File","");
          var path=document.all["File"+i.toString()].value;
          document.all["Text"+i.toString()].value=getname(path);
          }catch(e){
          }
          }
          function getname(path){
          var f1;
          var name;
          f1=path.split("\\");
          name=f1[f1.length-1];
          return name;
          }
          function Text_onfocus(e) {
          try{
          i=e.name.replace("Text","");
          var path=document.all["File"+i.toString()].value;
          if(document.all["Text"+i.toString()].value=="") 
          document.all["Text"+i.toString()].value=getname(path);
          }catch(e){
          }
          }
          //-->
          </script>
          </HEAD>
          <body>
          <FORM id="Form1" method="post" encType="multipart/form-data" runat="server">
          <FONT face="宋體">  文件上傳系統(tǒng)  
          <asp:TextBox id="TextBox1" runat="server" Width="24px" ToolTip="改變文本框的值可以增加上傳文件個數(shù).." MaxLength="2"
          AutoPostBack="True">1</asp:TextBox> 上傳文件個數(shù) 
          <TABLE id="TableMain" height="82" cellSpacing="0" cellPadding="4" width="500" border="1"
          runat="server" style="BORDER-COLLAPSE: collapse">
          <TR>
          <TD width="203" height="28">文件名稱(默認為文件名)</TD>
          <TD height="28">選擇上傳的文件</TD>
          </TR>
          <TR>
          <TD align="right" colSpan="2"> 
          <asp:Button id="Button1" runat="server" Text=" 保存 " Width="72px"></asp:Button>     
          </TD>
          </TR>
          </TABLE>
          </FONT>
          <asp:Label id="Label1" runat="server" Width="500px">注意:文件總大小不能超過4M..</asp:Label>
          </FORM>
          </body>
          </HTML>
          ______________________________________________________________________________________________
          答4:
          //upload.aspx.cs
          using System;
          using System.Collections;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Web;
          using System.Web.SessionState;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.HtmlControls;
          using System.IO;
          using RiseGuideClassLibrary;
          namespace RiseGuide.MegaNet.admin
          {
          /// <summary>
          /// upload 的摘要說明。
          /// </summary>
          public class upload : System.Web.UI.Page
          {
          protected System.Web.UI.WebControls.Button Button1;
          protected System.Web.UI.WebControls.Label Label1;
          protected System.Web.UI.WebControls.TextBox TextBox1;
          protected System.Web.UI.HtmlControls.HtmlTable TableMain;
          int FileCount=1;
          private void Page_Load(object sender, System.EventArgs e)
          {
          Response.Clear();
          this.Label1.Text ="提示:上傳文件大小總和不能超過4M..";
          try
          {
          FileCount=Int32.Parse(this.TextBox1.Text);
          }
          catch
          {
          this.TextBox1.Text="1";
          FileCount=1;
          }
          if(!IsPostBack)
          {
          CreateInputFile();
          }
          }
          #region Web 窗體設(shè)計器生成的代碼
          override protected void OnInit(EventArgs e)
          {
          //
          // CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
          //
          InitializeComponent();
          base.OnInit(e);
          }
          /// <summary>
          /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
          /// 此方法的內(nèi)容。
          /// </summary>
          private void InitializeComponent()
          {    
          this.TextBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
          this.Button1.Click += new System.EventHandler(this.Button1_Click);
          this.Load += new System.EventHandler(this.Page_Load);
          }
          #endregion
          /// <summary>
          /// 創(chuàng)建文件選擇框
          /// </summary>
          private void CreateInputFile()
          {
          while(TableMain.Rows.Count >2) 
          {
          TableMain.Rows.RemoveAt(TableMain.Rows.Count-1);//清除代碼產(chǎn)生的行
          }
          for(int i=0;i<FileCount;i++)
          {
          int pos=TableMain.Rows.Count-1;  //倒數(shù)第二行
          HtmlTableRow row=new HtmlTableRow();
          HtmlTableCell cell1=new HtmlTableCell();
          HtmlInputText txt=new HtmlInputText();
          txt.ID = "Text"+(i+1).ToString();
          txt.Attributes["onfocus"]="javascript:Text_onfocus(this)";
          cell1.Controls.Add(txt);
          HtmlTableCell cell2=new HtmlTableCell();
          HtmlInputFile file=new HtmlInputFile();
          file.ID=  "File"+(i+1).ToString();
          txt.Attributes["onblur"]="javascript:File_onblur(this)";
          cell2.Controls.Add(file);
          row.Cells.Add(cell1);
          row.Cells.Add(cell2);
          //插入生成的行
          this.TableMain.Rows.Insert(pos,row);
          }
          //this.Label1.Text ="提示:當前創(chuàng)建了"+this.TextBox1.Text+"個文件上傳框";
          }
          private void Button1_Click(object sender, System.EventArgs e)
          {
             HttpFileCollection f=Request.Files;
          this.Label1.Text ="提示:";
          if(Request.TotalBytes>4096000)  //判斷輸入大小
          {
          this.Label1.Text ="提示:上傳文件內(nèi)容總和大于了4M,請重新選擇文件";
          CreateInputFile();
          return ;
          }
          for(int i=0;i<f.Count ;i++)
          {
          if(Request.Form["File"+(i+1).ToString()]=="")
          continue;
          if(f[i].FileName =="") 
          {
          this.Label1.Text ="提示:請選擇您要上傳的文件";
          continue;
          }
          string name="";
          try
          {
          string[] names=f[i].FileName.Split('\\');
          name=names[names.Length-1];
          f[i].SaveAs(Server.MapPath("../photo")+"\\"+name);
          if(Request.Form["Text"+(i+1).ToString()]=="")
          SysPhoto(name.Split('.')[0]+DateTime.Now.ToString(),name);
          else
          SysPhoto(Request.Form["Text"+(i+1).ToString()],name);
          this.Label1.Text += f[i].FileName +" 上傳 成功..<BR>";
          }
          #if DEBUG
          catch(Exception err)
          {
          this.Label1.Text += f[i].FileName +" 上傳 失敗.."+err.Message+"<BR>";
          }
          #else
          catch
          {
          this.Label1.Text += f[i].FileName +" 上傳 失敗..<BR>";
          }
          #endif
          }
          CreateInputFile();
          }
          private void TextBox1_TextChanged(object sender, System.EventArgs e)
          {
          CreateInputFile();
          }
          /// <summary>
          /// 保存到數(shù)據(jù)庫
          /// </summary>
          private void SysPhoto(string name,string url)
          {
          dataBase db=new dataBase();
          db.ExecuteSql("insert into Sysphoto (name,url) values ('"+name+"','"+url+"')");
          }
          }
          }

          posted on 2012-08-28 12:07 ** 閱讀(1048) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          收藏夾

          C#學(xué)習(xí)

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 水富县| 武隆县| 北碚区| 甘德县| 扶绥县| 乌兰察布市| 嘉义县| 河西区| 苏尼特右旗| 潞城市| 多伦县| 崇明县| 交城县| 宜兰市| 垣曲县| 温宿县| 关岭| 凭祥市| 大冶市| 安远县| 库车县| 郁南县| 海盐县| 澎湖县| 淄博市| 庄河市| 武胜县| 邢台市| 洪雅县| 广平县| 平和县| 道孚县| 会宁县| 饶平县| 靖州| 新晃| 安国市| 忻州市| 翼城县| 兴隆县| 莎车县|