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

目录
相关文章
|
2月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
176 3
|
6月前
|
存储 XML 开发工具
【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
本文介绍了如何在Azure上使用App Service作为反向代理,以自定义域名访问Storage Account。主要内容包括: 1. **设置反向代理**:通过配置`applicationhost.xdt`和`web.config`文件,启用IIS代理功能并设置重写规则。 2. **验证访问**:测试原生URL和自定义域名的访问效果,确保两者均可正常访问Storage Account。 3. **.NET SDK连接**:使用共享访问签名(SAS URL)初始化BlobServiceClient对象,实现通过自定义域名访问存储服务。
|
8月前
|
开发框架 数据可视化 .NET
.NET 中管理 Web API 文档的两种方式
.NET 中管理 Web API 文档的两种方式
130 14
|
9月前
|
算法 Java 测试技术
Benchmark.NET:让 C# 测试程序性能变得既酷又简单
Benchmark.NET是一款专为 .NET 平台设计的性能基准测试框架,它可以帮助你测量代码的执行时间、内存使用情况等性能指标。它就像是你代码的 "健身教练",帮助你找到瓶颈,优化性能,让你的应用跑得更快、更稳!希望这个小教程能让你在追求高性能的路上越走越远,享受编程带来的无限乐趣!
397 13
|
10月前
|
API C#
在.NET中使用QuestPDF高效地生成PDF文档
在.NET中使用QuestPDF高效地生成PDF文档
247 0
|
Ubuntu 持续交付 API
如何使用 dotnet pack 打包 .NET 跨平台程序集?
`dotnet pack` 是 .NET Core 的 NuGet 包打包工具,用于将代码打包成 NuGet 包。通过命令 `dotnet pack` 可生成 `.nupkg` 文件。使用 `--include-symbols` 和 `--include-source` 选项可分别创建包含调试符号和源文件的包。默认情况下,`dotnet pack` 会先构建项目,可通过 `--no-build` 跳过构建。此外,还可以使用 `--output` 指定输出目录、`-c` 设置配置等。示例展示了创建类库项目并打包的过程。更多详情及命令选项,请参考官方文档。
653 13
|
存储 运维
.NET开发必备技巧:使用Visual Studio分析.NET Dump,快速查找程序内存泄漏问题!
.NET开发必备技巧:使用Visual Studio分析.NET Dump,快速查找程序内存泄漏问题!
230 2
|
自然语言处理 C# 图形学
使用dnSpyEx对.NET Core程序集进行反编译、编辑和调试
使用dnSpyEx对.NET Core程序集进行反编译、编辑和调试
186 0
|
11月前
|
XML 存储 安全
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
749 0
一款.NET开源、跨平台的DASH/HLS/MSS下载工具
一款.NET开源、跨平台的DASH/HLS/MSS下载工具
121 1