Home  >  Q&A  >  body text

javascript - How to convert json data to CSV file and download it

1. Use in react
2. Accept json data from the background and convert json to CSV
3. The user clicks the button button to download the CSV file

How to achieve this?

PHP中文网PHP中文网2671 days ago1171

reply all(1)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-26 10:55:11

    No react involved

    const data = "..." // 这里填CSV内容的字符串
    const blob = new Blob([data], {type: "text/plain"})
    const link = document.createElement("a")
    link.href = URL.createObjectURL(blob)
    link.download = "filename.csv" // 这里填保存成的文件名
    link.click()
    URL.revokeObjectURL(link.href)

    reply
    0
  • Cancelreply