• GridView 中下载文件


    GridView 中每一行都有一个File Name 列,显示文件名称,点击后可下载.

    前台GridView :

    <asp:TemplateField HeaderText="File Name" SortExpression="FULLFILENAME">
                      <itemtemplate>
                        <asp:LinkButton id="lbnFileName" onclick="lbnFileName_Click" runat="server" Text='<%# Eval("FULLFILENAME") %>'></asp:LinkButton>
                        <asp:Label id="lblGUIDFILENAME" runat="server" Text='<%# Eval("GUID_FILENAME") %>' Visible="False"></asp:Label>
                        <asp:Label id="lblTYPENAME" runat="server" Text='<%# Eval("TYPENAME") %>' Visible="False"></asp:Label>
                      </itemtemplate>
    </asp:TemplateField> 

    后台cs:

     

    代码
    protected void lbnFileName_Click(object sender, EventArgs e)
        {
            
    try
            {
                GridViewRow objGridViewRow 
    = (GridViewRow)((LinkButton)sender).NamingContainer;

                
    string strDQAManualFolder = Server.MapPath(@"~\DOC\Manual");
                
    string strFileName = ((LinkButton)objGridViewRow.FindControl("lbnFileName")).Text;
                
    string strGuid = ((Label)objGridViewRow.FindControl("lblGUIDFILENAME")).Text;
                
    string strFileType = ((Label)objGridViewRow.FindControl("lblTYPENAME")).Text;

                String fileName 
    = HttpUtility.UrlEncode(strFileName);
                FileInfo file 
    = new FileInfo(strDQAManualFolder + "\\" + strGuid + '.' + strFileType);
                
    if (file.Exists == false)
                {
                    
    this.ClientScript.RegisterStartupScript(this.GetType(), "FileNotFound""alert('System can not find the file!');"true);
                    
    return;
                }
                
    switch (strFileType)
                {
                    
    case "doc":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "application/octet-stream";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "ppt":
                    
    case "pps":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "application/vnd.ms-powerpoint";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "xls":
                    
    case "csv":
                    
    case "xml":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "application/vnd.ms-excel";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "pdf":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "application/pdf";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "htm":
                    
    case "html":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "text/html";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "gif":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "image/gif";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "jpg":
                    
    case "jpeg":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "image/jpeg";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "rtf":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "application/rtf";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    case "asp":
                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);
                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "text/asp";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;

                    
    default:

                        Response.Clear();
                        
    //Response.HeaderEncoding = System.Text.;
                        
    //欲讓使用者看到的檔案名稱
                        Response.AddHeader("Content-Disposition""filename=" + fileName);

                        
    //檔案大小
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        
    //檔案類型
                        Response.ContentType = "application/octet-stream";
                        
    //顯示檔案下載視窗
                        Response.TransmitFile(file.FullName);
                        Response.End();
                        
    break;
                }

            }
            
    catch (Exception ex)
            {
                
    this.ShowMessage("Download function exception!");
                Response.Write(ex.ToString());
            }

        }
  • 相关阅读:
    jquery及相关兼容插件的版本搭配
    jquery IE7 下报错:SCRIPT257: 由于出现错误 80020101 而导致此项操作无法完成
    兼顾php和jquery新版本的checkbox的写法
    jquery.validate动态更改校验规则 【转】
    Google的10大座右铭
    用jQuery屏蔽掉按回车键时提交表单
    sizeof()和strlen()在求字符串长度时的差别
    Matlab使用心得
    C++ 数据类型及相关问题 及输出精度控制
    原生javascript满屏上下滚动
  • 原文地址:https://www.cnblogs.com/andycai/p/1624749.html
Copyright © 2020-2023  润新知