asp.net利用一般处理程序下载和在线查看文档
2012-01-25 09:29
721 查看
传输文件路径给一般处理程序
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;
}
}
}
}
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;
}
}
}
}
相关文章推荐
- Asp.net利用一般处理程序实现文件下载功能
- asp.net一般处理程序利用反射定位方法
- 利用ASP.NET一般处理程序动态生成Web图像
- asp.net使用一般处理程序实现文件下载
- asp.net一般处理程序利用反射定位方法
- ASP.NET表单和一般处理程序处理文件上传和下载
- 利用ASP.NET一般处理程序动态生成Web图像(转)
- asp.net使用jbox插件,利用ajax(使用一般处理程序方式)执行撤销操作
- 【ASP.NET 进阶】无刷新上传图片之一:利用一般处理程序
- 文件上传 下载 一般处理程序和webForm联系以及asp.net内置对象
- ASP.NET 一般处理程序-下载
- 使用 ASP.NET 一般处理程序或 WebService 返回 JSON
- asp.net一般处理程序(2)获取列表
- ASP.NET图片防盗链(使用一般处理程序)
- ASP.NET 一般处理程序
- Android调用asp.net编写的*.ashx(一般处理程序)获取数据
- ASP.NET的ASHX一般处理程序中Session为null且不能赋值
- asp.net 中一般应用处理程序使用session
- Asp.net jQuery与ashx 安全性问题 (一般处理程序的安全问题) ashx 安全
- asp.net中一般处理程序获取session