Jason Van Der Meijden
Jason Van Der Meijden

Reputation: 155

Compressing a string in C# asp.net and decompressing it in JavaScript

I have a string, it is about 200 000 characters long. There is a lot of repetition in this string. I was wondering if there was a way that I could compress this string server side (c#) and the once I receive it client side (JS) that I decompress this string.

An example of the data: enter image description here

Upvotes: 2

Views: 1599

Answers (2)

Hans Kesting
Hans Kesting

Reputation: 39274

I don't know how well Javascript itself (through libraries) can use compressed data, but it doesn't need to.

When you GZip the data server side and set the "Content-Encoding" header value to "gzip", then the browser will decompress it before handing it over to your js code. So you have the benefit of small transport size and fast decompression.

Check beforehand whether the browser can do it, by checking the request header value "Accept-Encoding". If that contains "gzip" (which is usual), then the browser will decode it.

Further reading (specific for WebApi): http://blog.developers.ba/asp-net-web-api-gzip-compression-actionfilter/ and http://benfoster.io/blog/aspnet-web-api-compression

Upvotes: 2

MariusUt
MariusUt

Reputation: 752

Yes, you can pick any compression algorithm you want, implement that algorithm in C# to compress, and then again in javascript to decompress. Changing between C# and JavaScript does not cause you any difficulties, except the fact that you need to implement it in both languages of course.

I recommend you search for LZ compression algorithms.

Upvotes: 1

Related Questions