Failed to delete file on Windows using javascript in node.js

I have the following code that is trying to delete a file on a Windows 7 machine:

    // check if this item has an uploaded image file
    var imageFullPathName = __dirname + "/../public/images/" + req.params.itemId;
    logger.log("imageFullPathName = " + imageFullPathName);
    var normalizedPathName = path.normalize(imageFullPathName);
    logger.log("normalizedPathName = " + normalizedPathName);

    // delete the image if it exists
    fs.exists(normalizedPathName, function(exists) {
        console.log("Found the file: " + normalizedPathName);
        normalizedPathName = normalizedPathName.replace(/\\/g,"\\\\");
        console.log("New path name = " + normalizedPathName);
        fs.unlink(normalizedPathName, function(err){
            if (err){
                console.error("Error in call to fs.unlink");
            }
        });
    });

I get the following output:

imageFullPathName = C: \ IronKey \ HES \ cscie-71 \ Project \ medical interchange \ routes /../ public / images / 5658e5612103cb2c41000006

normalizedPathName = C: \ IronKey \ HES \ cscie-71 \ Project \ medical interchange \ Public \ images \ 5658e5612103cb2c41000006

New path name = C: \\ IronKey \\\\ HES cscie-71 \\ Project \\ medical-interchange \\\\ public images \\ 5658e5612103cb2c41000006

Error calling fs.unlink

, DOS. , , . , normalizedPathName.replace(), . , ​​, :

fs.unlink( "C:\\IronKey\\\\ HES cscie-71\\\\-\\\\ \\5658e5612103cb2c41000006", function (err) {...

, ? .

josh3736 , ENOENT. , , EXACT , :

        // check if this item has an uploaded image file
        var imageFullPathName = path.join(__dirname, "../public/images",
                                          sanitize(req.params.itemId));
        logger.log("imageFullPathName = " + imageFullPathName);

        fs.unlink(imageFullPathName, function(err){
            //if (err.code != 'ENOENT'){
            if (err){
                logger.log("Error in call to fs.unlink", err);
            }
            else{
                logger.log("No file found");
            }
            logger.log("Delete Success");
        });

: fs.unlink {[: ENOENT: , C:\\IronKey\\hes\\cscie-71\\project\\medical-interchange\\public\\images\\5658fd27fca1b3bc3d00000e ']

+4
1

.

  • . URL- itemId %2E%2E%2F%2E%2E%2F%2E%2E%2FWindows. Express req.params ../../../Windows, .

    , ( , HTTP, ). - sanitize-filename, , .

  • fs.exists - -. exists , . unlink - err.code == 'ENOENT', ; .

  • normalizedPathName = normalizedPathName.replace(/\\/g,"\\\\");? , (C:\foo\bar β†’ C:\\foo\\bar), , , .

  • path.join, . (\ vs /), .

,

var imageFullPathName = path.join(
                                    __dirname,
                                    "../public/images",
                                    sanitizeFilename(req.params.itemId)
                                );

fs.unlink(imageFullPathName, function(err){
    if (err) {
        if (err.code != 'ENOENT') {
            // handle actual errors here
            console.error("Error in call to fs.unlink", err);
        }
        // else there was no file, handle that if you need to
    }
    // else delete success, handle that if you need to
});

, replace:

. ? .

+2

Source: https://habr.com/ru/post/1617798/


All Articles