2

业务场景

通过input以json格式上传配置,直接读取并加载。再将数据通过json格式下载在本地。

代码

<input type="file" accept="application/json", class="upload"/>

$('.upload').click(function(e){
    var target = e.target
    var file = targe.files[0]
    var reader = new FileReader()
    reader.readAsText(file);
    reader.onload = function(){
        var result = JSON.parse(this.result)[0]
    }
})

<input type="button", class="download">

$('.download').click(function(){
    var d = {}
    var datastr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(d));
    var downloadAnchorNode = document.createElement('a')
    downloadAnchorNode.setAttribute("href", datastr);
    downloadAnchorNode.setAttribute("download", 'tem.json')
    downloadAnchorNode.click();
    downloadAnchorNode.remove();
})

努力奋斗的小菲菲
23 声望3 粉丝

小鲜肉一枚