简体   繁体   English

通过覆盖上一个来在Javascript中保存json文件

[英]Saving a json file in Javascript by overwrighting the previous one

I am a beginner in JS and trying to generate a json format file and saving it through this code 我是JS的初学者,并尝试生成json格式的文件并通过此代码保存

<script>
    var jsonArr=[];
//Generating 5 random test data for json array
for(var i = 0; i < 5; i++) {
    jsonArr.push({
        team: "teamA",
        matchname: "Match1",
        score: "32"
    });  } 
json_str = JSON.stringify(jsonArr); //stringify json

//Save file function
saveFile('yourfilename.json', "data:application/json", new Blob([json_str],{type:""}));

function saveFile (name, type, data) {
    if (data != null && navigator.msSaveBlob)
        return navigator.msSaveBlob(new Blob([data], { type: type }), name);

    var a = $("<a style='display: none;'/>");
    var url = window.URL.createObjectURL(new Blob([data], {type: type}));
    a.attr("href", url);
    a.attr("download", name);
    $("body").append(a);
    a[0].click();
    setTimeout(function(){  // fixes firefox html removal bug
        window.URL.revokeObjectURL(url);
        a.remove();
    }, 500);  
}

</script>

This code is working but i want to save this file to a specific directory with overwriting the previous saved file.If anybody can help? 该代码有效,但我想将此文件保存到特定目录并覆盖以前保存的文件。是否有人可以提供帮助? Thanks 谢谢

It isn't possible with Javascript to access the file system of the client. 使用Javascript无法访问客户端的文件系统。 You can only prompt the user with a download dialog or force a download to the default download directory. 您只能通过下载对话框提示用户或将下载强制到默认下载目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM