zoukankan      html  css  js  c++  java
  • 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());
            }

        }
  • 相关阅读:
    C# UrlDecode将+替换为空格问题
    Hashtable无序,用Dictionary代替
    Oracle查找Web执行SQL
    远程连接Oracle服务器
    asp.net core网站SSL nginx配置
    Supervisor踩过的坑
    centos nginx配置支持WebSocket(signalR)
    SignalR在asp.net core下使用
    Hangfire 在asp.net core环境的使用
    liteUploader上传控件的封装使用
  • 原文地址:https://www.cnblogs.com/andycai/p/1624749.html
Copyright © 2011-2022 走看看