Créer un fichier json en utilisant blob

J’ai écrit le code JSON en chaîne et je veux l’envoyer en utilisant xmlhttp en tant que fichier .json. Est-il possible de le faire avec blob?

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

Maintenant json à blob?

Essayez quelque chose comme ça

 var cleanScript = { 'type': 'script', 'api_key': api_key, 'data': data, 'inputs': inputs, 'timeoutSeconds': timeoutSeconds }; var jsonse = JSON.ssortingngify(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); 
 

Essayez le code ci-dessous:

  var int2ByteArray = function(i, minByteCount) { var result = [], buf = code = +i, offsetCount = 0; while ((buf = code>>(8 * offsetCount)) || offsetCount < minByteCount) { buf = buf & 0xFF; ++offsetCount; result.push(buf); } return result.reverse(); }; var ascii2ByteArray = function(s) { if (!s) return 0; var result = []; [].map.call(s, function(c) { result = result.concat(int2ByteArray((typeof(c)).toLowerCase() == "number" ? c : c.charCodeAt(0))); }); return result; }; // You got the blob here, do whatever you want. var blob = new Blob(new Uint8Array(ascii2ByteArray(jsonse)), {type:"text/json"}); 

La masortingce consiste à convertir une chaîne (chaînée par JSON.ssortingngify ) en un Uint8Array pouvant être utilisé pour la création d'un blob. Il m'est arrivé de faire quelque chose comme ça avant, en espérant que ce soit utile.