1

I'm trying to deflate a .xlsx file on the front end and inflate it in the server side, in a asp net core 2.2 server.

I tried everything and i have this now:

//JS code
handleSaveFile = (file) => {
    var compressedFile = pako.deflate(JSON.stringify(file), { to: 'string' });
    this.setState({ file: compressedFile });
  } 

Completely straight forward, pako.deflate is enough to do the trick.

Now on the back-end i tried everything but according to the documentation ends up like this: enter image description here

I tried also GZipStream, but the result is the same. I cant find anything regarding compress/decompress but there is plenty of info regarding the other way around.

Júlio Almeida
  • 1,539
  • 15
  • 23
  • 1
    What's the documentation you referred to? It would be better if you share the complete view code that can reproduce the issue(include how to call the server-side method from js). – Xueli Chen Mar 31 '20 at 05:53
  • There not much to say, the pako compression is one line of code. Works just fine in the server because i already have it working without compression. What i'm now doing is trying to deflate the body in the server, until now without success. – Júlio Almeida Apr 04 '20 at 06:06
  • Is there any update? I also stuck in this problem – Tethys Zhang Jul 15 '20 at 01:39
  • kinda ignored the situation because my use case doesn't require a file bigger than 25 mb, but eventually will come about. But for sure is not an easy task – Júlio Almeida Jul 15 '20 at 05:06

1 Answers1

2

Please take a look this, hope will help:

Client:

let output = pako.gzip(JSON.stringify(obj));

Server:

public static string Decompress(byte[] data)
{
    // Read the last 4 bytes to get the length
    byte[] lengthBuffer = new byte[4];
    Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
    int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);

    var buffer = new byte[uncompressedSize];
    using (var ms = new MemoryStream(data))
    {
        using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
        {
            gzip.Read(buffer, 0, uncompressedSize);
        }
    }
    string json = Encoding.UTF8.GetString(buffer); 
    return json;
}

detail in : https://stackoverflow.com/a/66825721/1979406

Haryono
  • 2,184
  • 1
  • 21
  • 14