怎样在asp.net中用一般处理文件ashx实现下载功能
我通过ajax将文件的完整名称作为参数传入到ashx处理文件中,现在想通过这个一般处理文件,将传入的文件下载下来,这个该怎么实现?最好贴上代码非常感谢!!!...
我通过ajax将文件的完整名称作为参数传入到ashx处理文件中,现在想通过这个一般处理文件,将传入的文件下载下来,这个该怎么实现?
最好贴上代码
非常感谢!!! 展开
最好贴上代码
非常感谢!!! 展开
2个回答
展开全部
01.<%@ WebHandler Language="C#" Class="download" %>
02.using System;
03.using System.Web;
04.public class download : IHttpHandler {
05.
06. public void ProcessRequest (HttpContext context) {
07. string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
08. downloadfile(url);
09. }
10.
11. public bool IsReusable {
12. get {
13. return false;
14. }
15. }
16. public void downloadfile(string s_fileName)
17. {
18. HttpContext.Current.Response.ContentType = "application/ms-download";
19. string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
20. System.IO.FileInfo file = new System.IO.FileInfo(s_path);
21. HttpContext.Current.Response.Clear();
22. HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
23. HttpContext.Current.Response.Charset = "utf-8";
24. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
25. HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
26. HttpContext.Current.Response.WriteFile(file.FullName);
27. HttpContext.Current.Response.Flush();
28. HttpContext.Current.Response.Clear();
29. HttpContext.Current.Response.End();
30. }
31.}
02.using System;
03.using System.Web;
04.public class download : IHttpHandler {
05.
06. public void ProcessRequest (HttpContext context) {
07. string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
08. downloadfile(url);
09. }
10.
11. public bool IsReusable {
12. get {
13. return false;
14. }
15. }
16. public void downloadfile(string s_fileName)
17. {
18. HttpContext.Current.Response.ContentType = "application/ms-download";
19. string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
20. System.IO.FileInfo file = new System.IO.FileInfo(s_path);
21. HttpContext.Current.Response.Clear();
22. HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
23. HttpContext.Current.Response.Charset = "utf-8";
24. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
25. HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
26. HttpContext.Current.Response.WriteFile(file.FullName);
27. HttpContext.Current.Response.Flush();
28. HttpContext.Current.Response.Clear();
29. HttpContext.Current.Response.End();
30. }
31.}
追问
这样写的话我测试了下,没反应
用ajax传过来文件fullname经过这样处理之后,在客户端页面中的ajax回调函数中是不是要做什么操作才行?
我现在是这样写的:
$.post("ajax/DownloadFileHandler.ashx",{"filePath":filePath},function(data,status){});
追答
不用。07行改为 string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["filePath"]);
试下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询