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

vojdkbi0  于 12个月前  发布在  Ionic
关注(0)|答案(1)|浏览(162)
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")
  });

db2dz4w8

db2dz4w81#

可以使用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);

字符串

相关问题