uniapp 在 APP平台,本地缓存图片

8次阅读
没有评论

共计 873 个字符,预计需要花费 3 分钟才能阅读完成。

const downPic = async (url: string, fileName: string) => {
    // 判断文件是否存在
    const isExist = () => {
      return new Promise((resolve, reject) => {
        plus.io.resolveLocalFileSystemURL(
          `_downloads/${fileName}`,
          function (entry) {
            // 文件存在
            console.log(`File exists: ${entry.name}`);
            resolve(entry);
          },
          function (e) {
            // 文件不存在
            console.log(`File not found: ${e.message}`);
            resolve(false);
          }
        );
      });
    };
    const file = await isExist();
    if (file) {
      return new Promise((resolve, reject) => {
        resolve({
          filename: `_downloads/${fileName}`
        });
      });
    }
    // eslint-disable-next-line no-async-promise-executor
    return new Promise((resolve, reject) => {
      const dtask = plus.downloader.createDownload(
        url,
        {
          filename: `_downloads/${fileName}`
        },
        function (d, status) {
          // 下载完成
          if (status === 200) {
            console.log(`Download success: ${d.filename}`);
            resolve(d);
          } else {
            console.log(`Download failed: ${status}`);
            reject(status);
          }
        }
      );
      dtask.start();
    });
  };

调用

// 调用
downPic(图片地址, md5(图片地址)).then((res: any) => {
          res.filename; // 图片地址
        });
正文完
 0
Eric chan
版权声明:本站原创文章,由 Eric chan 于2023-08-21发表,共计873字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。