Using javascript to download file as a.csv file

前端 未结 6 1462
难免孤独
难免孤独 2020-12-01 06:54

Hi I am trying to export a file as .csv file, so that when the user clicks on the download button, the browser would automatically download the file as .csv. I also want to

6条回答
  •  广开言路
    2020-12-01 07:16

    in modern browsers there is a new attribute in anchors.

    download

    http://caniuse.com/download

    so instead of using

    window.open("data:text/csv;charset=utf-8," + escape(csv))
    

    create a download link:

    download
    

    another solution is to use php

    EDIT

    i don't use jQuery, but you need to edit your code to add the download link with something like that in your function.

    var csv=ConvertToCSV(jsonObject),
    a=document.createElement('a');
    a.textContent='download';
    a.download="myFileName.csv";
    a.href='data:text/csv;charset=utf-8,'+escape(csv);
    document.body.appendChild(a);
    

提交回复
热议问题