Save to Local File from Blob

后端 未结 4 1236
梦如初夏
梦如初夏 2020-12-01 15:44

I have a difficult question to you, which i\'m struggeling on for some time now.

I\'m looking for a solution, where i can save a file to the users computer, without

4条回答
  •  不知归路
    2020-12-01 16:40

    this node dependence was more utils fs-web;

    npm i fs-web
    

    Usage

    import * as fs from 'fs-web';
    
    async processFetch(url, file_path = 'cache-web') {
        const fileName = `${file_path}/${url.split('/').reverse()[0]}`;
        let cache_blob: Blob;
        await fs.readString(fileName).then((blob) => {
          cache_blob = blob;
        }).catch(() => { });
        if (!!cache_blob) {
          this.prepareBlob(cache_blob);
          console.log('FROM CACHE');
        } else {
          await fetch(url, {
            headers: {},
          }).then((response: any) => {
            return response.blob();
          }).then((blob: Blob) => {
            fs.writeFile(fileName, blob).then(() => {
              return fs.readString(fileName);
            });
            this.prepareBlob(blob);
          });
     }
    

    }

提交回复
热议问题