jDownload是jQuery的一个下载插件,用户可以在下载文件之前知道文件的详细信息,在提高用户体验度方面起到了很大的作用。
鉴于官网的Demo是通过PHP文件来获取资源信息,对于ASP.NET开发人员来说不是很容易理解,所以本人写了一个ASP.NET的版本希望对jQuery有爱的童鞋们有所帮助。
一、首先添加一个测试页面Default.aspx,引用测试脚本和样式。
<
link rel
=
"
stylesheet
"
href
=
"
http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/base/jquery-ui.css
"
/
>
< script type = " text/javascript " src = " http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js " >< / script>
< script type = " text/javascript " src = " http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js " >< / script>
< link rel = " stylesheet " href = " /jquery.jDownload.css " / >
< script type = " text/javascript " src = " /jquery.jDownload.js " >< / script>
< script type = " text/javascript " >
$(document).ready( function () {
/* basic usage */
$( ' .download ' ).jDownload();
});
< / script>
< script type = " text/javascript " src = " http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js " >< / script>
< script type = " text/javascript " src = " http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js " >< / script>
< link rel = " stylesheet " href = " /jquery.jDownload.css " / >
< script type = " text/javascript " src = " /jquery.jDownload.js " >< / script>
< script type = " text/javascript " >
$(document).ready( function () {
/* basic usage */
$( ' .download ' ).jDownload();
});
< / script>
在body里添加测试代码
<
div
>
< p >
< a href ="images/hubble1.jpg" class ="download" title ="这是图片1" > Download here </ a ></ p >
< p >
< a href ="images/hubble2.jpg" class ="download" title ="这是图片2" > Download here </ a ></ p >
< p >
< a href ="images/hubble3.jpg" class ="download" title ="这是图片3" > Download here </ a ></ p >
< a id ="download" class ="download" href ="ReportTemplates/测试.xls" > 导出 </ a >
</ div >
< p >
< a href ="images/hubble1.jpg" class ="download" title ="这是图片1" > Download here </ a ></ p >
< p >
< a href ="images/hubble2.jpg" class ="download" title ="这是图片2" > Download here </ a ></ p >
< p >
< a href ="images/hubble3.jpg" class ="download" title ="这是图片3" > Download here </ a ></ p >
< a id ="download" class ="download" href ="ReportTemplates/测试.xls" > 导出 </ a >
</ div >
二、添加一个提供获取信息及下载用的页面jdownload.aspx,在cs文件中编写以下代码。
1
using System;
2 using System.IO;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Json;
5 using System.Text;
6 using System.Web;
7
8 namespace JDownload
9 {
10 public partial class jdownload : System.Web.UI.Page
11 {
12 string action = string.Empty;
13 string path = string.Empty;
14
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 action = Request.QueryString[ " action " ];
18 path = Server.UrlDecode(Request.QueryString[ " path " ]);
19
20 if ( ! String.IsNullOrEmpty(path))
21 {
22 switch (action)
23 {
24 case " download " :
25 GetFile(path);
26 break ;
27
28 case " info " :
29 GetInfo(path);
30 break ;
31 }
32 }
33 Response.Write(GetJson( new MyFileInfo() { Error = " error " }));
34 Response.End();
35 }
36
37 private void GetFile(string path)
38 {
39 // get file info
40 FileInfo fi = new FileInfo(Server.MapPath(path));
41 string fileName = fi.Name; // 客户端保存的文件名
42 string filePath = Server.MapPath(path); // 路径
43 // 以字符流的形式下载文件
44 FileStream fs = new FileStream(filePath, FileMode.Open);
45 byte [] bytes = new byte [( int )fs.Length];
46 fs.Read(bytes, 0 , bytes.Length);
47 fs.Close();
48 Response.ContentType = " application/octet-stream " ;
49 // 通知浏览器下载文件而不是打开
50 Response.AddHeader( " Content-Disposition " , " attachment; filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
51 Response.BinaryWrite(bytes);
52 Response.Flush();
53 Response.End();
54 }
55
56 private void GetInfo(string path)
57 {
58 // get file info
59 FileInfo fi = new FileInfo(Server.MapPath(path));
60 MyFileInfo myFileInfo = new MyFileInfo()
61 {
62 FileName = fi.Name,
63 FileType = fi.Extension,
64 FileSize = (fi.Length / long .Parse( " 1024 " )).ToString(),
65 Error = " null "
66 };
67 Response.Write(GetJson(myFileInfo));
68 Response.End();
69 }
70
71
72 public static string GetJson < T > (T obj)
73 {
74 DataContractJsonSerializer json = new DataContractJsonSerializer( typeof (T));
75 using (MemoryStream ms = new MemoryStream())
76 {
77 json.WriteObject(ms, obj);
78 string szJson = Encoding.UTF8.GetString(ms.ToArray());
79 return szJson;
80 }
81 }
82
83 [DataContract]
84 class MyFileInfo
85 {
86 [DataMember]
87 public string FileName { get; set; }
88 [DataMember]
89 public string FileType { get; set; }
90 [DataMember]
91 public string FileSize { get; set; }
92 [DataMember]
93 public string Error { get; set; }
94 }
95 }
96 }
2 using System.IO;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Json;
5 using System.Text;
6 using System.Web;
7
8 namespace JDownload
9 {
10 public partial class jdownload : System.Web.UI.Page
11 {
12 string action = string.Empty;
13 string path = string.Empty;
14
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 action = Request.QueryString[ " action " ];
18 path = Server.UrlDecode(Request.QueryString[ " path " ]);
19
20 if ( ! String.IsNullOrEmpty(path))
21 {
22 switch (action)
23 {
24 case " download " :
25 GetFile(path);
26 break ;
27
28 case " info " :
29 GetInfo(path);
30 break ;
31 }
32 }
33 Response.Write(GetJson( new MyFileInfo() { Error = " error " }));
34 Response.End();
35 }
36
37 private void GetFile(string path)
38 {
39 // get file info
40 FileInfo fi = new FileInfo(Server.MapPath(path));
41 string fileName = fi.Name; // 客户端保存的文件名
42 string filePath = Server.MapPath(path); // 路径
43 // 以字符流的形式下载文件
44 FileStream fs = new FileStream(filePath, FileMode.Open);
45 byte [] bytes = new byte [( int )fs.Length];
46 fs.Read(bytes, 0 , bytes.Length);
47 fs.Close();
48 Response.ContentType = " application/octet-stream " ;
49 // 通知浏览器下载文件而不是打开
50 Response.AddHeader( " Content-Disposition " , " attachment; filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
51 Response.BinaryWrite(bytes);
52 Response.Flush();
53 Response.End();
54 }
55
56 private void GetInfo(string path)
57 {
58 // get file info
59 FileInfo fi = new FileInfo(Server.MapPath(path));
60 MyFileInfo myFileInfo = new MyFileInfo()
61 {
62 FileName = fi.Name,
63 FileType = fi.Extension,
64 FileSize = (fi.Length / long .Parse( " 1024 " )).ToString(),
65 Error = " null "
66 };
67 Response.Write(GetJson(myFileInfo));
68 Response.End();
69 }
70
71
72 public static string GetJson < T > (T obj)
73 {
74 DataContractJsonSerializer json = new DataContractJsonSerializer( typeof (T));
75 using (MemoryStream ms = new MemoryStream())
76 {
77 json.WriteObject(ms, obj);
78 string szJson = Encoding.UTF8.GetString(ms.ToArray());
79 return szJson;
80 }
81 }
82
83 [DataContract]
84 class MyFileInfo
85 {
86 [DataMember]
87 public string FileName { get; set; }
88 [DataMember]
89 public string FileType { get; set; }
90 [DataMember]
91 public string FileSize { get; set; }
92 [DataMember]
93 public string Error { get; set; }
94 }
95 }
96 }
三、注意事项
在使用 System.Runtime.Serialization.Json 时需要引用System.ServiceModel.Web的DLL
源码下载Click Me