2

I have a question for you regarding GZip.

I have some dynamic forms created in .Net. Now I would like to have my form data (html + java script) gzipped and kept in database. so when the user request the form (JQuery Ajax) i can serve the gzipped version. I tried with GZipStream.but had no luck.

Any thoughts??

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

    byte[] byteArray = new byte[value.Length];
    byteArray = encoding.GetBytes(value);

    //Prepare for compress
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);

    //Compress
    sw.Write(byteArray, 0, byteArray.Length);
    //Close, DO NOT FLUSH cause bytes will go missing...
    sw.Close();

    System.Text.StringBuilder sB = new System.Text.StringBuilder();
    byte[] finalData = ms.ToArray();
    ms.Close();
    sw.Dispose();
    ms.Dispose();
    sB.Append(encoding.GetString(finalData));

    //Response.ClearContent();
    //Response.OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length);

    return sB.ToString();

Thanks, Shiras

Shiras
  • 115
  • 1
  • 1
  • 7
  • 3
    Please show some code. Why did you not have luck? What errors do you get and where? There is just no way to help you without additional information... – DerApe Jul 24 '13 at 05:23
  • Please see the comment updated. – Shiras Jul 24 '13 at 07:06
  • Still we do not know, how you call the method and what exactly went wrong (is the sb empty? what do you expect on client side? how do you process the data?). You can't just post some code and say "I had no luck" – DerApe Jul 24 '13 at 07:24
  • The above code is wrapped in a method. which is static method. This method is called from JQuery AJAX. The datatype is JSON. The sB.ToString() is not empty. it has encrypted value. WHAT I AM EXPECTING IS INSTEAD OF THE SERVER DOING THE GZIP. i WILL DO THE GZIP AND THEN SEND TO THE CLIENT (HTML PAGES WHICH IS HAVING JQUERY AJAX CALLS) THEN THE BROWSER WHEN IT RECEIVES i AM EXPECTING THE BROWSER WILL AUTOMATICALLY DECODE. Now when the data reaches the client (HTML pages which raised the AJAX calls) it is not decoding automatically, instead it is showing the encrypted data. – Shiras Jul 24 '13 at 07:48
  • So you think shouting will help? Since there are multiple components involved (html, javascript, c# code, database, etc) just posting one part of the call chain (which actually does work as intended...) will not help us to figure out your problem. If you send gziped data down to the client, you need to decode it there too. It's not magically done for you. – DerApe Jul 24 '13 at 07:58
  • Hi derape, The important parts i marked in CAPS. there was no intention of shouting. I AM SORRY FOR THAT. I am wondering so how does IIS do the compression and the Browser decompress it automatically? – Shiras Jul 24 '13 at 10:48
  • Well in that case, I do not know so much about the response encoding. I guess it has something to do with the response header, but I guess I can't help you here. I did some quick search and found this: http://stackoverflow.com/questions/1730182/generate-and-serving-gz-compressed-in-asp-net . Did you specify your `ContentType` correctly? – DerApe Jul 24 '13 at 11:00

0 Answers0