2014-04-17 11 views
0

在下面的代碼中,我在會話中獲得附件文件名我已經綁定了documnetid,attachmentname和download.but當我選擇第一個attachmnet文件名時,它正在下載第二個attchmt文件名。我的目標是下載我在網格視圖中選擇的內容。 如在網格視圖下載選定的文件名

document id attchfilename download 
1    xxx    view 
2    yyy    view 

標記

<asp:GridView ID="Attchdwnld" runat="server" 
     AllowPaging="true" 
     AllowSorting="true" 
     AlternatingRowStyle-CssClass="alt" 
     AutoGenerateColumns="false" 
     CellPadding="4" 
     CssClass="mGrid" 
     ForeColor="#333333" 
     PageSize="10" 
     PageSize-Mode="NumericPages" 
     PagerStyle-CssClass="pgr" 
     PagerStyle-Visible="true" 
     ShowFooter="false" 
     Width="100%" 
     OnRowCommand="Attchdwnld_RowCommand" > 
    <Columns> 
     <asp:TemplateField HeaderText="DocumentID" ItemStyle-Width="200px"> 
      <ItemTemplate> 
       <asp:Label ID="DocumentID" runat="server" Text='<%#Eval("DocumentID") %>'> 
       </asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="AttachmentFileName" ItemStyle-Width="200px"> 
      <ItemTemplate> 
       <asp:Label ID="AttachmentFileName" runat="server" Text='<%#Eval("AttachmentFileName") %>'> 
       </asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="DownLoad" ItemStyle-Width="150px"> 
      <ItemTemplate> 
       <asp:LinkButton ID="lnlbtnAttch" runat="server" Text="View" CommandName="Edit"> 
       </asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <HeaderStyle Font-Bold="True" ForeColor="White" /> 
</asp:GridView> 

代碼隱藏

protected void Attchdwnld_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    var SearchDoc = (SearchDoc)Session["Documentname"]; 
    lblMessage.Text = SearchDoc.AttachmentFileName; 
    string fileurl = "C:\\Search\\" + stName + "\\" + strtFolder + "\\" + lblMessage.Text; 
    string filename = fileurl; 
    if (filename != "") 
    { 
     string path = filename; 
     System.IO.FileInfo file = new System.IO.FileInfo(path); 
     if (file.Exists) 
     { 
      Response.Clear(); 
      Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
      Response.AddHeader("Content-Length", file.Length.ToString()); 
      Response.ContentType = "application/octet-stream"; 
      Response.WriteFile(file.FullName); 
      Response.End(); 
     } 
     else 
     { 
      Response.Write("This file does not exist."); 
     } 
    } 
} 
+0

你在哪裏設置'會話[ 「Documentname」]'的價值? –

回答

0

我不明白爲什麼你應該在你的代碼存儲Session["Documentname"]可言。所以我省略了那部分。

試試這個代碼

protected void Attchdwnld_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    // gets the clicked row of your GridView 
    var row = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow; 
    //gets the filename in the cliked row 
    var attachmentNameLabel = row.FindControl("AttachmentFileName") as Label; 
    //store it to lblMessage's Text 
    lblMessage.Text = attachmentNameLabel.Text; 
    var filePath = String.Format("C:\\Search\\{0}\\{1}\\{2}", 
     stName, strtFolder, lblMessage.Text); 

    var file = new System.IO.FileInfo(filePath); 
    if (file.Exists) 
    { 
     Response.Clear(); 
     Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
     Response.AddHeader("Content-Length", file.Length.ToString()); 
     Response.ContentType = "application/octet-stream"; 
     Response.WriteFile(file.FullName); 
     Response.End(); 
    } 
    else 
    { 
     Response.Write("This file does not exist."); 
    } 
} 
+0

下載不起作用 – user3492682

+0

最新錯誤?文件是否存在?你在哪個瀏覽器上測試它?還有你在'lblMessage.Text'中顯示的內容 – naveen

+0

謝謝我解決了這個問題 – user3492682