jQuery UI Download
生活随笔
收集整理的這篇文章主要介紹了
jQuery UI Download
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
jDownload是jQuery的一個(gè)下載插件,用戶(hù)可以在下載文件之前知道文件的詳細(xì)信息,在提高用戶(hù)體驗(yàn)度方面起到了很大的作用。
鑒于官網(wǎng)的Demo是通過(guò)PHP文件來(lái)獲取資源信息,對(duì)于ASP.NET開(kāi)發(fā)人員來(lái)說(shuō)不是很容易理解,所以本人寫(xiě)了一個(gè)ASP.NET的版本希望對(duì)jQuery有愛(ài)的童鞋們有所幫助。
一、首先添加一個(gè)測(cè)試頁(yè)面Default.aspx,引用測(cè)試腳本和樣式。
<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>
在body里添加測(cè)試代碼
<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/測(cè)試.xls">導(dǎo)出</a>
</div>
二、添加一個(gè)提供獲取信息及下載用的頁(yè)面jdownload.aspx,在cs文件中編寫(xiě)以下代碼。
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;//客戶(hù)端保存的文件名
42 ? string filePath = Server.MapPath(path);//路徑
43 ?//以字符流的形式下載文件
44 ? FileStream fs =new FileStream(filePath, FileMode.Open);
45 byte[] bytes =newbyte[(int)fs.Length];
46 fs.Read(bytes, 0, bytes.Length);
47 fs.Close();
48 Response.ContentType ="application/octet-stream";
49 //通知瀏覽器下載文件而不是打開(kāi)
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 }
三、注意事項(xiàng)
在使用 System.Runtime.Serialization.Json?時(shí)需要引用System.ServiceModel.Web的DLL
源碼下載Click Me
轉(zhuǎn)載于:https://www.cnblogs.com/zhuwenjun/archive/2011/03/11/jDownload.html
總結(jié)
以上是生活随笔為你收集整理的jQuery UI Download的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: liunx mysql端口没开_linu
- 下一篇: php mysql 截取字符串长度,my