压缩、解压 解决 客户端查询大批量数据时等待时间过长的问题
??? 在項(xiàng)目中查詢時(shí),因數(shù)據(jù)量大,導(dǎo)致網(wǎng)絡(luò)傳輸很慢,這就需要在服務(wù)器端查詢出的數(shù)據(jù)進(jìn)行壓縮處理,后傳輸完了在客戶端進(jìn)行解壓處理(此為在Silverlight中壓縮與解壓); 具體方法如下:
using Newtonsoft.Json; using Telerik.Windows.Zip; ////服務(wù)器端對(duì)查詢出的數(shù)據(jù)進(jìn)行壓縮處理 public static string CompressString(string str) {string result = string.Empty;try{MemoryStream memoryStream = new MemoryStream();ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Default);StreamWriter writer = new StreamWriter(zipOutputStream);writer.Write(str);writer.Flush();result = Convert.ToBase64String(memoryStream.ToArray());}catch { }return result;}如:Common.CompressString(JsonConvert.SerializeObject(b.ToList<Employees>()));服務(wù)器端查詢出來(lái)的 b.ToList<Employees>()數(shù)據(jù)后,先序列化后通過(guò)上面方法CompressString()壓縮;壓縮后傳輸?shù)娇蛻舳?#xff0c;此Employees為一個(gè)實(shí)體類。
////客戶端進(jìn)行解壓處理 public string UnCompressString(string compressedBase64String){string result = string.Empty;try{MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(compressedBase64String));ZipInputStream input = new ZipInputStream(memoryStream);StreamReader reader = new StreamReader(input);result = reader.ReadToEnd();}catch { }return result;}如: ? result_Employees= new QueryableCollectionView(JsonConvert.DeserializeAnonymousType<List<Employees>>(UnCompressString(e.Result),new List<Employees>()));客戶端接受數(shù)據(jù) 時(shí)先通過(guò)方法UnCompressString()解壓數(shù)據(jù)后反序列化,得到相關(guān)實(shí)體類Employees的List表;
?? 通過(guò)序列化、壓縮、解壓、反序列化,會(huì)發(fā)現(xiàn)在處理大批量數(shù)據(jù)查詢時(shí),客戶端查詢等待時(shí)間會(huì)縮短幾十倍的關(guān)系;
??? 值得注意的是,在用WebServices作為服務(wù)器查詢數(shù)據(jù)時(shí),當(dāng)前臺(tái)頁(yè)面引用該WebServices時(shí)可能丟失實(shí)體類的定義說(shuō)明,故在引用的Reference.cs中需加上該實(shí)體類的定義說(shuō)明,否則客戶端解壓數(shù)據(jù)時(shí)出問(wèn)題。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaozou1018/archive/2013/01/10/2854788.html
總結(jié)
以上是生活随笔為你收集整理的压缩、解压 解决 客户端查询大批量数据时等待时间过长的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: “华实骤舒结”上一句是什么
- 下一篇: 【文摘】 雪念——作者:蓝色妖姬