簡體   English   中英

在網格視圖中下載選定的文件名

[英]To download selected filename in grid view

在下面的代碼中,我在會話中獲取了附件文件名,我綁定了documnetid,attachmentname和download。但是當我選擇第一個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.");
        }
    }
}

我完全看不出為什么要在代碼中存儲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.");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM