I'm trying to create a file and then writing in it and it says it creates but when I go to the folder there's nothing there.
can somebody tell me what am I doing wrong?
Here's my code:
this.file.createFile(this.file.dataDirectory, array.ERROR + '_error', true).then(() => {
console.log("Created the file!" + this.file.dataDirectory.toString());
}).catch(err => console.log(err));
let blob_toSend = new Blob([this.inputxml], { type: 'text/plain' });
this.file.writeFile(this.file.dataDirectory, array.ERROR + '_error', blob_toSend, {replace: true, append: false}).then(() => {
console.log("Wrote the file!");
}).catch(err => console.log(err));
I tried to give it a name like "_error.txt" but it doesn't work it either. The file is not showing. Thanks in advance
In you ts file:
export class HomePage {
fileValue:string;
constructor(private plt:Platform,private file: File) {}
checkValueExist(){
return (this.fileValue);
}
createFile(){
if(this.checkValueExist()){
this.file.createFile(this.file.dataDirectory,this.fileValue,true).then(xfile=>{
let filePath = xfile.nativeURL.substr(0, xfile.nativeURL.lastIndexOf('/') + 1);
if (this.plt.is('android')) {
this.copyToAppAndroidDir(filePath, xfile.name, this.createFileName(xfile.name));
}else{
this.copyToAppIosDir(filePath, xfile.name, this.createFileName(xfile.name));
}
}).catch(err=>{
alert('File Not Created cause of : '+ JSON.stringify(err));
})
}else{
alert('empty text');
}
}
createFileName(name) {
var newFileName = name + ".txt";
return newFileName;
}
copyToAppAndroidDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, this.file.externalDataDirectory, newFileName).then(success => {
alert('done');
}, error => {
console.log('err :', error);
});
}
copyToAppIosDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, this.file.dataDirectory, newFileName).then(success => {
alert('done');
}, error => {
console.log('err :', error);
});
//here in the this.file.dataDirectory for ios it may work or could be tempDirectory since in reality i have android device and don't know where ios store its files so you can change when testing on ios and know the directory.
}
}
And in html :
<ion-input type="text" [(ngModel)]="fileValue"></ion-input>
<ion-button color="primary" shape="round" expand="block" (click)="createFile()">Create File</ion-button>