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.

Revisions

  1. nhattruongniit revised this gist Oct 14, 2022. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions download_data json javascript
    Original file line number Diff line number Diff line change
    @@ -12,3 +12,30 @@ function download_data(filename, data) {
    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);
  2. nhattruongniit created this gist Oct 12, 2022.
    14 changes: 14 additions & 0 deletions download_data json javascript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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);
    }
    }