如何上传多个文件并查看上传的文件?

时间:2018-12-03 06:35:31

标签: c# asp.net

我想查看上传的文件。我正在上传2个PDF,当我单击上载的(view)链接时,其中仅显示1个PDF。 问题仅viewing the uploaded file。我正在agreement folder中上传2个PDF文件,在那里我可以看到上传的文件。 但是,当我单击上载的(查看)链接时,它仅显示1个PDF文件。

https://ibb.co/S3WMmKf \

下面是我的代码。

页面加载:

ViewState["filename"] = agremtname;
if (dt.Rows[0]["agreement"].ToString() == "0")
{
    rbno.Checked = true;
    FileUpload1.Enabled = false;
}
else
{
    rbyes.Checked = true;
    lbluploadmsg.Text = "Uploaded(View)";
    agreefile.Attributes["href"] = "~/Agreements/"+ agremtname;             
}

源代码:

<tr><td class="style11" >Reply recieved date<br/>   <asp:TextBox ID="txtrecdate" Width="207"  class="datepicker" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator10"
            ValidationGroup="valBtoA" ControlToValidate="txtrecdate" runat="server" ForeColor="red" ErrorMessage="reply date!!"></asp:RequiredFieldValidator>
    </td>

    <td class="style3">
    Agreement
        <asp:RadioButton ID="rbyes" GroupName="agree"  AutoPostBack="true" OnCheckedChanged="enablefileuploader" runat="server" />
                  Yes
        <asp:RadioButton ID="rbno" GroupName="agree" AutoPostBack="true" OnCheckedChanged="disablefileuploader"  runat="server" />
                  No&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           <a id="agreefile" target="_blank" runat="server"><asp:Label ID="lbluploadmsg" ForeColor="blue" runat="server" ></asp:Label>
          </a>
      <br/>    
    </td>
    </tr>

      <tr>


    <td class="style10"> Agreement<br/> 
        <%--<asp:TextBox ID="txtagreement" runat="server" Width="119px"></asp:TextBox>--%>
        <asp:FileUpload ID="FileUpload1" runat="server" accept=".pdf,.PDF" AllowMultiple="true" />
       &nbsp;&nbsp; <asp:Label ID="lblfileupmsg" runat="server" ForeColor="red"></asp:Label>
        </td>
    <td class="style12">
    Comment2<br/> 
      <asp:TextBox ID="txtcomment2" TextMode="MultiLine" Width="207" runat="server"></asp:TextBox>       
    </td>
    </tr>

更新按钮代码:

if (rbyes.Checked)
{
     if (FileUpload1.HasFile)
     {

         agreement = "1";
         filename = Path.GetFileName(FileUpload1.FileName);
         string fileLocation = Server.MapPath("~/Agreements/" + filename);
         FileUpload1.SaveAs(fileLocation);

     }
     else {
         filename = ViewState["filename"].ToString();
         if (filename == "")
         {
             lblfileupmsg.Text = "Please Upload Agreement file!!!";
             return;
         }
         agreement = "1";
     }

 }
 else if (rbno.Checked)
 {
     filename = "";
     agreement = "0";
 }

我知道要查看多个文件,我必须在page load中使用循环,但是我没有获得实现它的方法。

1 个答案:

答案 0 :(得分:0)

您必须使用count变量来计算文件数量,还必须将多个文件名保存在变量中,并使用,首先通过逻辑将它们分开。

int Count = 0;

每个循环的内部

     agreement = "1";
     filename = Path.GetFileName(FileUpload1.FileName);
     string fileLocation = Server.MapPath("~/Agreements/" + filename);
     FileUpload1.SaveAs(fileLocation);
     count++
  if (count > 1)
            {
               string multiplefilename  += "," + filename;
            }
            else if (count == 1)
            {
                string multiplefilename = filename;
            }

检索文件以获取视图

首先用分割多文件名,然后将结果保存在字符串数组和

string[] Array = recc.MultipleFile.Split(',');

检索文件,然后使用for循环。