閱讀700 返回首頁    go 阿裏雲 go 技術社區[雲棲]


asp.net利用一般處理程序下載和在線查看文檔

 

傳輸文件路徑給一般處理程序

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandArgument != null
                && Utility.IsNumber(e.CommandArgument.ToString()))
            {
                // 獲取操作的文檔的id
                int id = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName)
                {
                    case "btnDownLoad":
                        Response.Redirect("../Tool/Download.ashx?path="
                            + "\\Docs\\" + PassageListBLL.GetPassageListByID(id).Title);
                        break;
                }
            }
        }

 

下載

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tool;

namespace FullTextSearchDemo.Ajax
{
    /// <summary>
    /// Download 的摘要說明
    /// </summary>
    public class Download : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (context.Request.QueryString["path"] != null
                    && context.Request.QueryString["path"].ToString().Trim() != "")
                {
                    string path = context.Request.QueryString["path"].ToString();
                    System.IO.FileInfo file
                        = new System.IO.FileInfo

                          (System.Web.HttpContext.Current.Server.MapPath(path));
                    context.Response.Clear();
                    context.Response.Charset = "GB2312";
                    context.Response.ContentEncoding = System.Text.Encoding.UTF8;

                    // 添加頭信息,為"文件下載/另存為"對話框指定默認文件名,

                       設定編碼為UTF8,防止中文文件名出現亂碼
                    context.Response.AddHeader
                        ("Content-Disposition",
                        "attachment; filename="
                        + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));

                    // 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
                    context.Response.AddHeader("Content-Length", file.Length.ToString());

                    //// 指定返回的是一個不能被客戶端讀取的流,必須被下載
                    //context.Response.ContentType = "application/ms-excel";

                    // 把文件流發送到客戶端
                    context.Response.WriteFile(file.FullName);

                    // 停止頁麵的執行
                    context.Response.End();
                }
                else
                {
                    JScript.Alert("您下載的資源不存在!");
                }


            }

            catch
            {
                JScript.Alert("您下載的操作有誤,請重新再試!");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

在線查看

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tool;

namespace FullTextSearchDemo.Ajax
{
    /// <summary>
    /// Download 的摘要說明
    /// </summary>
    public class Download : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (context.Request.QueryString["path"] != null
                    && context.Request.QueryString["path"].ToString().Trim() != "")
                {
                    string path = context.Request.QueryString["path"].ToString();
                    System.IO.FileInfo file
                        = new System.IO.FileInfo

                          (System.Web.HttpContext.Current.Server.MapPath(path));
                    context.Response.Clear();
                    context.Response.Charset = "GB2312";
                    context.Response.ContentEncoding = System.Text.Encoding.UTF8;

                    // 添加頭信息,為"文件下載/另存為"對話框指定默認文件名,

                       設定編碼為UTF8,防止中文文件名出現亂碼
                    context.Response.AddHeader
                        ("Content-Disposition",
                        "online; filename="
                        + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));

                    // 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
                    context.Response.AddHeader("Content-Length", file.Length.ToString());

                    //// 指定返回的是一個不能被客戶端讀取的流,必須被下載
                    //context.Response.ContentType = "application/ms-excel";

                    // 把文件流發送到客戶端
                    context.Response.WriteFile(file.FullName);

                    // 停止頁麵的執行
                    context.Response.End();
                }
                else
                {
                    JScript.Alert("您下載的資源不存在!");
                }


            }

            catch
            {
                JScript.Alert("您下載的操作有誤,請重新再試!");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

最後更新:2017-04-02 22:16:00

  上一篇:go FindControl——對母版頁使用FindControl方法
  下一篇:go getCurrentSession與openSession的區別