asp.net利用一般处理程序下载和在线查看文档-阿里云开发者社区

开发者社区> 开发与运维> 正文

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章