我应该在哪里将图像存储为 ArrayBuffer

时间:2021-05-20 04:51:45

标签: javascript google-chrome-extension

我正在制作一个 chrome 扩展,用户可以在其中将计算机中的图像设置为背景。到目前为止,我能够将图像存储为 chrome.storage.local 中的 ArrayBuffer,但最多只能存储 5MB。这限制了我的用户可以上传的照片。

在这种情况下,是否还有其他地方可以存储图像或从磁盘读取图像?

这是我从用户设备读取文件的方式

  async handleFile() {
      const options = {
        types: [
          {
            description: "Image",
            accept: {
              "image/*": [".jpg"],
            },
          },
        ],
      };
      let [fileHandle] = await window.showOpenFilePicker(options);
      const file = await fileHandle.getFile(); // once a user picks an image return the path of that image
      //  Lets let the user do the reader on the load
      let reader = new FileReader();
      reader.addEventListener(
        "load",
        function () {
          chrome.storage.sync.set({ background: false }, () => {
            console.log(reader.result);
            chrome.storage.local.set({ image: reader.result }, () => {
              chrome.tabs.query(
                { active: true, currentWindow: true },
                (tabs) => {
                  chrome.tabs.reload(tabs[0].id);
                }
              );
            });
          });
        },
        false
      );
      console.log(file);
      reader.readAsDataURL(file);
      // if (file.size < 5000000) {
      //   this.error = "";
      // } else {
      //   this.error = "File Size is too large";
      // }
    },

0 个答案:

没有答案
相关问题