读书人

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

发布时间: 2012-01-06 22:55:18 作者: rapoo

多文件上传获取上传控件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"> <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.";        }    } 


[解决办法]
<input type="text" name="附件名" id="附件ID" />
cs里 string Nmaes=Request.Forms["附件名"].ToString();
得到的Names就是你所有附件文本的值是用逗号隔开了的 你可以把他Spit(',')放在数组里处理
上传控件就和2楼那样处理 结合起来你要的目的就可以达到
[解决办法]

C# code
   HttpFileCollection c = Request.Files;        foreach (string key in c.AllKeys)        {            if (key.Substring(0, 4) == "main")            {                //是正文                c[key].SaveAs("");            }            else            {                //是附件                c[key].SaveAs("");            }        } 

读书人网 >asp.net

热点推荐