【问题标题】:Is there a way to store pdf files in firebase?有没有办法在 firebase 中存储 pdf 文件?
【发布时间】:2023-02-25 05:12:32
【问题描述】:

我正在尝试将 base-64 编码的 pdf 文件存储到 firestore 中,但是 base-64 文本太长而无法存储到数据库中有没有办法在存储之前压缩文本?

我首先尝试了主程序,这就是我得到的:

Uncaught (in promise) FirebaseError: Document 'projects/jeel-al-ebdaa/databases/(default)/documents/Teachers/testteacher' cannot be written because its size (1,377,477 bytes) exceeds the maximum allowed size of 1,048,576 bytes.

我知道这意味着文本太长了。 我找不到在纯 vanilla js 中压缩文本的方法

【问题讨论】:

  • 请帮忙,我需要尽快提交这个项目。
  • 您是否尝试过网络搜索“javascript 压缩字符串”?

标签: javascript firebase google-cloud-firestore browser


【解决方案1】:

是的,使用压缩和解压缩数据算法是可能的,为此有很多算法,但我建议使用DEFLATE algorithm 来压缩数据。 在 JavaScript 中,zlib 库用于压缩数据,它基于 DEFLATE 算法,您可以使用 npm 或 yarn 安装该库。

npm install zlib

之后,您创建压缩zlib.deflate()您的 pdf 大小的代码;

这是例子:-

const zlib = require('zlib');

const buffer = Buffer.from(base64String, 'base64');

zlib.deflate(buffer, (err, compressedBuffer) => {
 if (err) {
   console. Error(err);
   return;
 }

   const compressedBase64String = compressedBuffer.toString('base64');

    // Store the compressed base-64 string in Firestore
    firestore.collection('myCollection').doc('myDoc').set({
    compressedPdf: compressedBase64String
    });
  });

并解压缩以使用zlib.inflate() 示例:-

const compressedBuffer = Buffer.from(compressedBase64String, 'base64');

// Decompress the buffer using the zlib library
zlib.inflate(compressedBuffer, (err, decompressedBuffer) => {
   if (err) {
    console. Error(err);
    return;
     }
  const decompressedBase64String = decompressedBuffer.toString('base64');

    });

【讨论】:

    猜你喜欢
    • 2017-05-16
    • 2021-12-18
    • 2020-10-01
    • 2020-08-11
    • 2021-08-15
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多