使用blob,如何通过从下面的代码获取respData来将文件保存到android本地目录

问题描述 投票:0回答:1
var blob = new Blob([resp.data],{type: resp.headers('Content-Type')});

我尝试使用下面的代码,但无法下载到本地目录

$cordovaFile.writeFile(pathFile, fileName, contentFile, true)
  .then(function(success) {
    //success
  }, function(error) {
    alert("error in the report creation")
  });
javascript ionic-framework blob
1个回答
0
投票

您可以使用

cordova-plugin-file

const blob = new Blob([resp.data], { type: resp.headers('Content-Type') });
const pathToFile = cordova.file.dataDirectory;
const fileName = 'file';

document.addEventListener('deviceready', async () => {
  try {
    const fileSystem = await new Promise((resolve, reject) => {
      window.resolveLocalFileSystemURL(pathToFile, resolve, reject);
    });

    const fileEntry = await new Promise((resolve, reject) => {
      fileSystem.getFile(fileName, { create: true, exclusive: false }, resolve, reject);
    });

    const fileWriter = await new Promise((resolve, reject) => {
      fileEntry.createWriter(resolve, reject);
    });

    fileWriter.onwriteend = () => {
      console.log('File saved successfully:', fileEntry.nativeURL);
    };

    fileWriter.onerror = (error) => {
      console.error('Error writing file:', error);
    };

    fileWriter.write(blob);
  } catch (error) {
    console.error('Error:', error);
  }
}, false);


© www.soinside.com 2019 - 2024. All rights reserved.