当前位置: 代码迷 >> ASP.NET >> 多文件上传获取上传控件id的有关问题
  详细解决方案

多文件上传获取上传控件id的有关问题

热度:6000   发布时间:2013-02-25 00:00:00.0
多文件上传获取上传控件id的问题
前台:正文1:<input name='MainFile1' id='MainFile1' size='52' class='txt_normal' type='file'>
  附件1:<input name='SecondFile1' id='SecondFile1' size='52' class='txt_normal' type='file'>
后台: HttpFileCollection files = HttpContext.Current.Request.Files; 
问题是如何在后台利用HttpFileCollection files = HttpContext.Current.Request.Files;保存文件的时候获取input控件的id,因为分正文和附件分别存放。

------解决方案--------------------------------------------------------
C# code
前台:<head runat="server">    <title>无标题页</title>    <script language="JavaScript">  function addFile()  {      var str = '<BR><INPUT type="file" size="50" NAME="File" runat="server">'      document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)  }  </script></head><body>    <form id="form1" runat="server">    <div>    <table class="fullwidth" align="center">                        <TR>        <TD vAlign="top">Attachment :</TD>        <TD>         <P id="MyFile"><input id="filMyFile" type="file" size="50" name="filMyFile">&nbsp;<input onclick="addFile()" type="button" value="Add"></P>         <asp:label id="lblAttachmentError" runat="server" ForeColor="Red"></asp:label><BR>         <asp:button id="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click"></asp:button><asp:label id="lblAttachment" runat="server"></asp:label></TD>       </TR></table>    </div>        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />        <asp:Label ID="Label1" runat="server" Width="119px"></asp:Label>    </form></body>后台:protected void btnUpload_Click(object sender, EventArgs e)    {        HttpFileCollection files = HttpContext.Current.Request.Files;        for (int i = 0; i < files.Count; i++)        {            if (i < files.Count && i < 10)            {                if (files[i].FileName != "" || files[i] != null)                {                    int FileSize = 6 * 1024 * 1024;                    HttpPostedFile myFile = files[i];                    string strFilePath = myFile.FileName.ToString().Trim();                    this.lblAttachmentError.Text = "<" + strFilePath + ">"; // Show file name                     int nFindSlashPos = strFilePath.Trim().LastIndexOf("\\") + 1;                    string UploadFileName = strFilePath.Substring(nFindSlashPos);                    string FileName = string.Format("{0:yyMMdd}", DateTime.Now) + "-" + UploadFileName;                    if (myFile.FileName.Trim() == "") // Empty value in Browse Box                    {                        this.lblAttachmentError.Text = "No file selected.";                        return;                    }                    if (myFile.ContentLength != 0)                    {                        if (myFile.ContentLength > FileSize)                        {                            this.lblAttachmentError.Text = "File Size is limited to 6 MB only.";                            return;                        }                        this.lblAttachment.Text += "<BR>" + FileName;                        this.lblAttachmentError.Text = "";                        myFile.SaveAs(@"D:\" + FileName);                    }                    else                    {                        this.lblAttachmentError.Text = "File not found.";                        return;                    }                }            }            else                this.lblAttachmentError.Text = "Uploaded File exceed limits.";        }    }
  相关解决方案