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

目录
相关文章
|
4月前
|
SQL 开发框架 .NET
分享130个ASP整站程序源码,总有一款适合您
分享130个ASP整站程序源码,总有一款适合您
39 1
|
4月前
|
SQL 开发框架 .NET
分享134个ASP整站程序源码,总有一款适合您
分享134个ASP整站程序源码,总有一款适合您
22 1
|
4月前
|
开发框架 .NET 网络安全
分享128个ASP整站程序源码,总有一款适合您
分享128个ASP整站程序源码,总有一款适合您
21 0
|
4月前
|
开发框架 安全 .NET
分享119个ASP整站程序源码,总有一款适合您
分享119个ASP整站程序源码,总有一款适合您
37 4
|
4月前
|
SQL 开发框架 .NET
分享123个ASP整站程序源码,总有一款适合您
分享123个ASP整站程序源码,总有一款适合您
23 0
|
4月前
|
开发框架 .NET UED
分享111个ASP整站程序源码,总有一款适合您
分享111个ASP整站程序源码,总有一款适合您
32 3
|
2天前
|
XML 开发框架 .NET
LabVIEW中加载.NET 2.0,3.0和3.5程序集
LabVIEW中加载.NET 2.0,3.0和3.5程序集
11 4
|
2天前
|
开发框架 .NET 开发工具
LabVIEW加载.NET程序集
LabVIEW加载.NET程序集
|
26天前
|
存储 移动开发 前端开发
对象存储oss使用问题之OSS SDK .net 使用下载例程报错如何解决
《对象存储OSS操作报错合集》精选了用户在使用阿里云对象存储服务(OSS)过程中出现的各种常见及疑难报错情况,包括但不限于权限问题、上传下载异常、Bucket配置错误、网络连接问题、跨域资源共享(CORS)设定错误、数据一致性问题以及API调用失败等场景。为用户降低故障排查时间,确保OSS服务的稳定运行与高效利用。
31 0
|
3月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
39 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!