String Extension Method Zip:
public static byte[] Zip(this string str)
{
var bytes = Encoding.ASCII.GetBytes(str);
using (var memorystreaminput = new MemoryStream(bytes))
using (var memorystreamoutput = new MemoryStream())
{
using (var gs = new GZipStream(memorystreamoutput, CompressionMode.Compress))
{
memorystreaminput.CopyTo(gs);
}
return memorystreamoutput.ToArray();
}
}
Repository Save:
var showsIQueryable = GetShowsIQueryable();
var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue };
var data = serializer.Serialize(showsIQueryable);
var compressedDataBlob = data.Zip();
//stores compressedDataBlob
Controller ActionResult Get:
var showsJsonText = _showService.CommandRepo.GetShowsCache();
Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "gzip");
return new FileContentResult(showsJsonText, "application/javascript");
JavaScript:
$.ajax({
type: "POST",
url: serverUrl,
dataType: 'json',
cache: false,
async: true,
data: JSON.stringify(model),
contentType: "application/json; charset=utf-8"
}).done(function (data) {
....
});
HTML:
<script type="text/javascript">
var shows = @Html.Raw(new JavaScriptSerializer().Serialize(Model.Shows)
</script>
No comments:
Post a Comment