I dont know why ,but i am not able to delete file using unlink()
and unlinksynk()
.
I have stored file using multiparty.Form()
method. It will generate file with random name and returns filePath
and originalFileName
. And after this i have renamed file with originalFileName
.
Code:
await form.parse(fileData, (err: Error, fields: any, res: any) => {
if (err) {
callback(err, null);
} else {
let file_path = res.file[0].path;
let assignedFilePath = file_path.substr(res.file[0].path.lastIndexOf('\/') + 1);
let originalFilePath = res.file[0].originalFilename;
originalFilePath = path.dirname(assignedFilePath) + '\\' + originalFilePath;
fs.rename(assignedFilePath, originalFilePath, function (err: any) {
if (err)
callback(err, null);
else
callback(err, JSON.stringify({'path': originalFilePath}));
});
}
});
Code for deleteFile()
deleteTempFiles(tempFolderPath: string) {
return new Promise(function (resolve: any, reject: any) {
fs.readdir(tempFolderPath, (err: any, files: any) => {
if (err) {
reject();
} else {
for (let file of files) {
fs.unlink(tempFolderPath + file, (err: any) => {
if (err) {
reject();
}
});
}
resolve();
}
});
}).catch(function (e) {
Promise.reject(e.message);
})
}
EDIT :
ERROR is, while deleting file ,compiler says Error: EBUSY: resource busy or locked, unlink
.
And I think i am not using the file anywhere in program ?
Thanks in advance .
==== 2nd answer ===
Your deleteTempFiles
function is utterly wrong
read this removing of each file in directory (and directories) inside a dir
You have many logical errors in the code.
.
and ..
- not possibleresolve
s after that loop, and rejects after 1st error - but usually that reject should be much after resolve (loop is fast, it only queues io access, not executing the unlinks yet, then resolve is executed).==== 1st answer ===
You have code for renameFile
, which moves file form old name(and location) into new one. Therefore deleting old file would not work at all so you should delete new file.