'How to overwrite an existing file while saving using javascript?

I am trying to save a file in javascript using the below code

var blob = new Blob([text], {type: "text/plain;charset=utf-8"});
saveAs(blob, fileName +"_"+unit+".json");

I want to overwrite any existing file with the same name as current file, with the current file without prompting the user.

For example if aaa_bbb.json exists in users download folder and I want save this one as aa_bbb.json it should directly overwrite the existing file instead of asking user to save the file by name aaa_bbb(1).

The complete function is as below :

function showPopup(Attributes){
var htmlString = getWindowContent(Attributes);
map.infoWindow.setTitle("<b>Site Name : </b>"+ fileName+"<br><b>Unit Name : </b>"+ unit+"<br><b>Site Area : </b>"+ polyArea+"<br><b>Select soil types & click  </b><button id=\"btn-save\" type=\"submit\">Save to file</button>");
map.infoWindow.setContent(htmlString);
map.infoWindow.resize(300,300);    
map.infoWindow.show(map.toScreen(currentClick), map.getInfoWindowAnchor(map.toScreen(currentClick)));

$("#btn-save").click(function() {
    var array = [];
        $("#tbl input[name='link']").each(function() {
          //for each checked checkbox, iterate through its parent's siblings
            subArray = $(this).parent().siblings().map(function() {
                return $(this).text().trim();
            }).get();
            var cokey = subArray[2];
            var mukey = subArray[3];
            if(array == null){
                var subArray = [];
                subArray.push(cokey);
                array.push({
                    "mukey":mukey,
                    "cokeyArray": subArray
                });
            }else{
                var bool = false;
                array.forEach(function(obj){
                    if(obj.mukey==mukey){
                        obj.cokeyArray.push(cokey);
                        bool = true;
                    }
                });
                if(bool == false){
                    var subArray = [];
                    subArray.push(cokey);
                    array.push({
                        "mukey":mukey,
                        "cokeyArray": subArray
                    });
                }
            }
           array.push(mukey);
        })
        //to print the value of array

    var text = getFinalJson(Attributes, array);
    var blob = new Blob([text], {type: "text/plain;charset=utf-8"});
    //get date for fileName
    getDateFileName();
    getParams(url);
    //console.log("Before save ",fileName,unit);
    saveAs(blob, fileName +"_"+unit+".json");
    setTimeout(window.location.href = "http://myaddress.com",2000);
});

Is it possible? If yes could someone please tell me how to do it?

Thanks



Solution 1:[1]

No. This is impossible. You can trigger a download with a suggested filename, but you can't pick files on the visitor's computer to simply overwrite. That would be a major security problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quentin