简体   繁体   中英

Decompress a GZIP CSV file on client side with Pako.js

Problem

I want to load a large gzipped CSV file using the Fetch API and Pako.js with client-side code. This is the code I was using:

const res = await fetch('www.example.com/large.csv.gzip');
let raw = await res.text();
raw = pako.inflate(raw);
console.log(raw);

I got an error without any stack trace:

Uncaught (in promise) unknown compression method

Attempted Research

I found some examples, but they didn't relate to CSV files, the Fetch API, or both:

Environment

Firefox 108.0.2 (64-bit)

Pako.js version 2.1.0

I came across a Codepen that was using XHR requests and setting the response type to an array buffer.

So I tried that with the Fetch API and the code worked!

const res = await fetch('www.example.com/large.csv.gzip');
let raw = await res.arrayBuffer();
raw = pako.inflate(raw);
console.log(raw); // Prints CSV file content successfully

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM