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