You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中无法加载压缩包内Blob格式文本问题求助

Troubleshooting FileReader Issues with Zipped TXT Blobs

Hey there, let's get to the bottom of why your FileReader isn't producing any output. The lack of logs or errors usually points to a few common issues—let's break them down and fix your code step by step.

First: Are you in the Browser or Node.js?

Wait a second—you mentioned installing FileReader via npm. FileReader is a built-in browser API, so you don't need to install it for browser code. If you're running this in Node.js, the npm filereader package is a polyfill that might not behave exactly like the browser version, and you're better off using Node's native file system tools instead. Let's cover both scenarios:

If you're working in the Browser:

1. Validate Your Blob First

Before trying to read the Blob, confirm it's actually valid. If the Blob is empty or wasn't properly extracted from the zip, FileReader won't have anything to process. Add logs to check its properties:

convertInText = (blob) => {
  console.log("Blob size:", blob.size);
  console.log("Blob type:", blob.type);
  if (blob.size === 0) {
    console.error("Blob is empty—check your zip extraction!");
    return;
  }
  // Rest of your code...
};

2. Fix the Encoding

"ANSI" isn't a standard encoding name recognized by FileReader. Most "ANSI" files use the iso-8859-1 encoding (or a Windows-specific codepage like windows-1252). Replace "ANSI" with a valid encoding:

reader.readAsText(blob, "iso-8859-1"); // Use this instead of "ANSI"

3. Add Error Handling

You're missing listeners for errors and aborts—this is why you're not seeing any feedback when something goes wrong. Add these to catch issues:

convertInText = (blob) => {
  let reader = new FileReader();
  
  reader.readAsText(blob, "iso-8859-1");
  
  reader.onloadend = function(e) {
    console.log("File content:", reader.result);
  };

  reader.onerror = function(e) {
    console.error("Error reading Blob:", e.target.error);
  };

  reader.onabort = function(e) {
    console.error("Blob reading aborted");
  };
};

4. Ensure the Zip Extraction is Complete

If you're using a zip library (like JSZip), make sure you're waiting for the extraction to finish before passing the Blob to convertInText. Most zip operations are asynchronous—for example:

// Example with JSZip
zip.loadAsync(zipBlob).then(function(zip) {
  return zip.file("your-file.txt").async("blob"); // Wait for Blob to be ready
}).then(function(txtBlob) {
  convertInText(txtBlob); // Now pass the valid Blob to your function
}).catch(function(err) {
  console.error("Zip extraction failed:", err);
});

If you're working in Node.js:

Forget the npm filereader package—use Node's native fs module or a zip library that can read text directly. For example, with adm-zip:

const AdmZip = require('adm-zip');

const zip = new AdmZip('your-zip-file.zip');
const txtContent = zip.readAsText('your-file.txt', 'iso-8859-1'); // Specify encoding
console.log(txtContent);

Or with unzipper (for async handling):

const unzipper = require('unzipper');
const fs = require('fs');

fs.createReadStream('your-zip-file.zip')
  .pipe(unzipper.Parse())
  .on('entry', function(entry) {
    if (entry.path === 'your-file.txt') {
      entry.read().toString('iso-8859-1', function(err, content) {
        if (err) throw err;
        console.log(content);
        entry.autodrain();
      });
    } else {
      entry.autodrain();
    }
  });

Final Notes

If you're still stuck, check if the TXT file inside the zip is actually readable outside your code—open it in a text editor to confirm it's not corrupted. That will rule out issues with the file itself.

内容的提问来源于stack exchange,提问作者Nelson Silva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:45:28