Skip to content

Instantly share code, notes, and snippets.

@nhattruongniit
Last active October 14, 2022 15:31
Show Gist options
  • Select an option

  • Save nhattruongniit/6d0b76751b5a3a24c7f11d3235a76df6 to your computer and use it in GitHub Desktop.

Select an option

Save nhattruongniit/6d0b76751b5a3a24c7f11d3235a76df6 to your computer and use it in GitHub Desktop.
function download_data(filename, data) {
var blob = new Blob([data], {type: 'application/json'});
if(window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
}
else{
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
OR
export default function getDate() {
const today = new Date();
const day = today.getDate() < 10 ? `0${today.getDate()}` : today.getDate();
const month = today.getMonth() + 1 < 10 ? `0${today.getMonth() + 1}` : `${today.getMonth() + 1}`;
const date = new Date();
const hour = addZero(date.getHours());
const minute = addZero(date.getMinutes());
const second = addZero(date.getSeconds());
const text = `${month}${day}${today.getFullYear()}_${hour}${minute}${second}`;
return text;
}
const jsonParser = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(dataSet, undefined, 2));
const a = document.createElement('a');
const nameSplited = nameJson.split(".")[0];
a.style.display = 'none';
a.href = jsonParser;
a.download = `${nameSplited}_${getDate()}.json`; // name json file
document.body.appendChild(a)
a.click();
window.URL.revokeObjectURL(jsonParser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment