使用blob创建json文件 [英] Create json file using blob

查看:372
本文介绍了使用blob创建json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字符串中编写了json代码,我希望使用xmlhttp作为.json文件发送它。可以用blob来做吗?

  var cleanScript = {
'type':'script',
'api_key':api_key,
'数据':数据,
'输入':输入,
'timeoutSeconds':timeoutSeconds
};
var jsonse = JSON.stringify(cleanScript,null,2);

现在json到blob?

尝试这样的事情

  var cleanScript = {
'type':'script',
'api_key':api_key,
'data':data,
'input':inputs,
'timeoutSeconds':timeoutSeconds
};
var jsonse = JSON.stringify(cleanScript);
var blob = new Blob([jsonse],{type:application / json});
var url = URL.createObjectURL(blob);

var a = document.createElement('a');
a.href = url;
a.download =backup.json;
a.textContent =Download backup.json;

document.getElementById('json')。appendChild(a);



 < div id = JSON >< / DIV> 


I have written json code in string and i want to send it using xmlhttp as .json file. Is it possible to do it with blob?

var cleanScript = {
    'type': 'script',
    'api_key': api_key,
    'data': data,
    'inputs': inputs,
    'timeoutSeconds': timeoutSeconds
};
var jsonse = JSON.stringify(cleanScript, null, 2); 

Now json to blob?

解决方案

Try something like this

var cleanScript = {
    'type': 'script',
    'api_key': api_key,
    'data': data,
    'inputs': inputs,
    'timeoutSeconds': timeoutSeconds
};
var jsonse = JSON.stringify(cleanScript);
var blob = new Blob([jsonse], {type: "application/json"});
var url  = URL.createObjectURL(blob);

var a = document.createElement('a');
a.href        = url;
a.download    = "backup.json";
a.textContent = "Download backup.json";

document.getElementById('json').appendChild(a);

<div id="json"></div>

这篇关于使用blob创建json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆