展开GridView单元以查看和下载文件

时间:2013-09-23 05:36:36

标签: c# javascript asp.net ajax gridview

我有一个gridview,在这个gridview中有一列“文件名”,其中包含文件名。 要求:当我点击特定文件名时,我应该能够看到文件的内容并能够保存或下载该文件。

所有方法都非常受欢迎。

_________________________________________
|                        |**file name** |
_________________________________________
|                        | a.txt        | <<----Click a.txt    
_________________________________________
|                        | b.txt        |
_________________________________________

此致 的Vivek

2 个答案:

答案 0 :(得分:0)

有一个链接按钮或按钮来显示文件名。使用runat =“server”放置div以显示文件内容。

处理文件名按钮单击事件。在这种情况下,请阅读文件内容并将其设置为div的InnerHTML。

以上是显示文字/ html内容。

答案 1 :(得分:0)

您可以尝试这样的方法,打开带有filename为queryparam的新popup.aspx页面。 在新的弹出页面page_load中,阅读内容并将其写入浏览器

<asp:GridView ID="FilterGrid" runat="server" AutoGenerateColumns="False" >
    <Columns>
        <asp:TemplateField HeaderText="FileName" >
            <ItemTemplate>
               <asp:HyperLink ID="ActionHyperLink" runat="server" Text='<%# Eval("FileName") %>' NavigateUrl='<%# Eval("FileName","~/FilePopUp.aspx?filename={0}") %>' Target="_blank" />
               <asp:HyperLink ID="HyperLink2" runat="server" Text='<%# Eval("FileName") %>' NavigateUrl='<%# String.Format("filepopup.aspx?filename={0}", Eval("filename")) %>' onclick="javascript:w= window.open(this.href,'DownloadFile','left=20,top=20,width=500,height=500,toolbar=0,resizable=0');return false;"></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

弹出窗口的

protected void Page_Load(object sender, EventArgs e)
{
            if (Request.QueryString["filename"] != null)
            {
                //Get the content from database
                byte[] fileContent = ....;
                Response.Clear();
                string doctype = ...;
                if (doctype == ".doc")
                {
                    Response.ContentType = "application/msword";
                }
                else if (doctype == ".pdf")
                {
                    Response.ContentType = "application/pdf";
                }
                else if (doctype == ".xls")
                {
                    Response.ContentType = "application/vnd.ms-excel";
                }
                else if (doctype == ".xlsx")
                {
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                }
                Response.BinaryWrite(fileContent);
            }
}
相关问题