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