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 plete 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 = "",2000);
});
Is it possible? If yes could someone please tell me how to do it?
Thanks
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 plete 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.",2000);
});
Is it possible? If yes could someone please tell me how to do it?
Thanks
Share Improve this question edited May 5, 2022 at 9:56 Quentin 945k133 gold badges1.3k silver badges1.4k bronze badges asked Sep 12, 2016 at 15:35 Hiranava DasHiranava Das 1152 silver badges6 bronze badges 5-
3
give that in-browser JS has no filesystem calls, perhaps you should explain what platform/library you're using to provide this
saveAs
call? We can't help you with a function call that isn't standard. – Marc B Commented Sep 12, 2016 at 15:36 - How would you feel if you visited a site and it overwrote the files on your puter without asking you? It's not possible, for that reason. – BadHorsie Commented Sep 12, 2016 at 15:39
- I have posted my plete function below. – Hiranava Das Commented Sep 12, 2016 at 15:41
- @BadHorsie Ya I agree. I still asked just in case. Can we remove the the "(1)" part from the suggested name ? – Hiranava Das Commented Sep 12, 2016 at 15:42
- @MarcB — It is part of the discontinued Writer API. – Quentin Commented Sep 12, 2016 at 15:42
1 Answer
Reset to default 7No. This is impossible. You can trigger a download with a suggested filename, but you can't pick files on the visitor's puter to simply overwrite. That would be a major security problem.