【问题标题】:Expand GridView cell to view and download file展开 GridView 单元格以查看和下载文件
【发布时间】:2013-09-27 22:37:03
【问题描述】:

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

所有方法都非常受欢迎。

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

问候, 维韦克

【问题讨论】:

  • 您需要以 pdf 格式显示文件还是只保存该文件?
  • 我什么都好。主要是我想在单击文件名时查看文件的内容,并且应该有一个保存文件内容的选项。文件格式不是我关心的问题。
  • 能否请您显示您的网格视图的内容..?

标签: c# javascript asp.net ajax gridview


【解决方案1】:

有一个链接按钮或按钮来显示文件名。放置一个带有 runat="server" 的 div 以显示文件内容。

处理文件名按钮点击事件。在事件中,读取文件内容并将其设置为 div 的 InnerHTML。

以上用于显示文本/html内容。

【讨论】:

  • Clint ID 硬件编号 文件名 序列号 100 5 abc.xml 1007 105 17 xyz.xml 5017
【解决方案2】:

你可以试试这样,打开新的 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>

popup的页面加载量

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);
            }
}

【讨论】:

  • 很抱歉提供的信息较少。文件内容来自数据库表。一个表有一列“文件内容”。因此,基于特定文件名的 Click Event,我将执行查询以从 DB 中获取文件内容。
  • 我添加了一个javascript版本,Hyperlink2控件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多