要上传21个文件,我就摆了21个 HtmlInputFile <input type="file">控件用SaveAs来实现。但这样就需要操作人员作21次浏览选择操作,而且这21个文件在同一目录下,且这个目录下文件个数上百,每一次浏览选择都都要在这上百文件中长时间寻找,很是烦琐,所以这个方法被领导和操作人员严重批评和否定了。
这21个文件是在同一个目录下的,并且文件名是已知的,请教如何只用一次浏览选择(或者只作尽可能少的操作),即可上传多个文件的方法!!PS:原本有个单纯的想法,因为文件名已知,所以只摆一个<input type="file">控件,用循环来改变PostedFile.FileName的值,后来才发现PostedFile.FileName是只读的......#·*!~#¥%

解决方案 »

  1.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C
      

  2.   

    /给你例子看看:  
    //注: 有些类你没有,可以暂时不使用,但不影响上传功能
    //page : 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="宋体">&nbsp; 文件上传系统&nbsp;&nbsp;
    <asp:TextBox id="TextBox1" runat="server" Width="24px" ToolTip="改变文本框的值可以增加上传文件个数.." MaxLength="2"
    AutoPostBack="True">1</asp:TextBox>&nbsp;上传文件个数&nbsp;
    <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">&nbsp;
    <asp:Button id="Button1" runat="server" Text=" 保存 " Width="72px"></asp:Button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </TD>
    </TR>
    </TABLE>
    </FONT>
    <asp:Label id="Label1" runat="server" Width="500px">注意:文件总大小不能超过4M..</asp:Label>
    </FORM>
    </body>
    </HTML>
    //page: unload.aspx.csusing 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 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </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>
    /// 创建文件选择框
    /// </summary>
    private void CreateInputFile()
    {
    while(TableMain.Rows.Count >2) 
    {
    TableMain.Rows.RemoveAt(TableMain.Rows.Count-1);//清除代码产生的行
    } for(int i=0;i<FileCount;i++)
    {
    int pos=TableMain.Rows.Count-1;  //倒数第二行
    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 ="提示:当前创建了"+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 ="提示:上传文件内容总和大于了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>
    /// 保存到数据库
    /// </summary>
    private void SysPhoto(string name,string url)
    {
    dataBase db=new dataBase();
    db.ExecuteSql("insert into Sysphoto (name,url) values ('"+name+"','"+url+"')");
    }
    }
    }
      

  3.   

    本文件用VB.NET编辑,在WIN2000+IIS(安装有.NET支持)中调试通过,可以上传许多常用的文件类型.如
    RAR、ZIP、DOC、TXT、JPG、GIF等等!
    直接下载保存为ASPX格式就可以
    ------------------------------------------------------------
    <html>
    <title>文件上传</title><body>
    <form enctype="multipart/form-data" runat=server>
    <Input type=file id=fileup runat=server size="20"><p>
    <asp:button id=upload_button onclick=uploadfile text="上传" runat=server/>
    </form>
    <p>
    <asp:label id=uptype runat=server/>
    </body>
    </html>
    <script language=VB runat=server>
      Sub uploadfile(sender as object,e as eventargs)
        If fileup.postedfile.contentlength=0 Then
         uptype.text="你还没有选择需要上传的文件!"
        Else
          Dim filesplit() as string=split(fileup.postedfile.filename,"\")
          Dim filename as string=filesplit(filesplit.length-1)
          fileup.postedfile.saveas(server.mappath(".")&"\"&filename)
          uptype.text="文件名称:"&fileup.postedfile.filename &"<br>"& _
        "文件大小:"&fileup.postedfile.contenttype &"<br>"& _
        "文件类型:"&fileup.postedfile.contentlength
        End If
        
      End Sub
    </script>