Downloading a CSV of file using Vue and JS

vue download file
vue-json-csv example
vue download file from assets
download csv file using ajax jquery
javascript read local csv file
how to write data into csv file in javascript
javascript read csv file line by line
javascript download csv

I have a field called csvdata that contains an array of the following format:

[
    [1,2,3],
    [4,5,6],
    [7,8,9]
]

I'm trying to download a CSV file of this data. I'm using Vue so it looks like:

<button type="button" class="btn btn-info action_btn" v-on:click="downloadCSVData">
      Download
</button>

How should the downloadCSVData function look like?


  1. Convert the Array to text. (let data = [[1,2,3], [4,5,6], [7,8,9]].join('\r\n').toString())
  2. Convert the text to base64. (let encoded_data = btoa(data))
  3. Create an iFrame and set its src = the encoded data. let iframe = document.createElement('iframe'); iframe.src = "data:application/octet-stream;base64," + encoded_data
  4. Append the iframe to the document's body: document.body.appendChild(iframe).

There's some drawbacks to this approach though:

  1. Content Security Policy can block step 4. You have control over CSP in your page, but don't sacrifice security for easy code.
  2. The file will always be named "download" with no file extension.

A better approach would be to create a temporary file on the server and provide the user a link to that file.

thatsus/vue-csv-downloader: A Vue.js button component to , vue-csv-downloader. A Vue.js link component to download a CSV file of client-​side data. The contents are a Font Awesome CSV icon by default, but they can be​  vue-blob-json-csv. Component library to download a JSON or CSV file using Vue.


I think you can create a method like so, assuming csvdata is a data property accessible by this in your Vue component:

downloadCSVData() => {
    let csvString = 'Put,Column,Titles,Here\n';
    this.csvdata.forEach((row) => {
            csv += row.join(',');
            csv += "\n";
    });
 
    let anchor = document.createElement('a');
    anchor.href = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv);
    anchor.target = '_blank';
    anchor.download = 'nameYourFileHere.csv';
    anchor.click();
}

VUE Export to CSV, button(@click="csvExport(csvData)") Export to CSV. 7. table. 8. tr. 9. th(v-for="th in ['name', 'username/email', 'address', 'phone', 'website', 'company']") {{th}}. 10. i will guide you to vue axios download file with example. you can download pdf file or zip file using vue js axios. if you need to download image or any file from url or blob in node js, react js etc then you can do it using axios js. we can also use get or post request for download file in vue js axios. it will also use with laravel vue download file.


Here is the working code to download a csv file in vue

This sample code converts array of objects to perfect csv file with headers call the function exportCSVFile(headers, items, fileTitle)

headers = { name: 'Name' // formatted column name, age: 'Age' }

items = [ { name: 'Joo', age: 21 }, { name: 'ant', age: 20 } ]

filename = 'somefilename.csv'

function convertToCSV(objArray) {
  const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
  let str = '';
  for (let i = 0; i < array.length; i++) { // eslint-disable-line
    let line = '';
    for (const index in array[i]) { // eslint-disable-line
      if (line !== '') line += ',';
      line += array[i][index];
    }
    str += line + '\r\n'; // eslint-disable-line
  }
  return str;
}

function exportCSVFile(headers, items, fileTitle) {
  if (headers) {
    items.unshift(headers);
  }
  const jsonObject = JSON.stringify(items);
  const csv = convertToCSV(jsonObject);
  const exportedFilenmae = fileTitle + '.csv' || 'export.csv'; // eslint-disable-line
  const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
  if (navigator.msSaveBlob) { // IE 10+
    navigator.msSaveBlob(blob, exportedFilenmae);
  } else {
    const link = document.createElement('a');
    if (link.download !== undefined) {
      const url = URL.createObjectURL(blob);
      link.setAttribute('href', url);
      link.setAttribute('download', exportedFilenmae);
      link.style.visibility = 'hidden';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }
}

export default exportCSVFile;

If the given value is 2d array, just use this function

function downloadCSVData () {
    var array = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
    ];
    var str = '';
    for (var i = 0; i < array.length; i++) { 
    let line = '';
    line = array[i].join(",");
    str += line + '\r\n';
    }
    var blob = new Blob([str], { type: 'text/csv;charset=utf-8;' });

    var link = document.createElement('a');
    var url = URL.createObjectURL(blob);
    link.setAttribute('href', url);
    link.setAttribute('download', 'csvfilename.csv');
    link.style.visibility = 'hidden';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

vue-json-csv, Download your JSON data as an CSV file directly from the browser. VueJS component to export Json Data into CSV file and download the resulting file. - Belphemur/vue-json-csv


vue-csv-downloader, vue-csv-downloader. A Vue.js link component to download a CSV file of client-side data. Install. npm install vue-csv-downloader --save-dev. Use. Attributes. Customization. Contribution. License. Reading csv file using JavaScript and HTML5 January 16, 2020 JsTutorials Team javascript CSV stands for comma-separated-values is the most popular file format to exchange information or data between cross programming languages.You can also use CSV to store information in spreadsheet or database.


Component library to download a JSON or CSV file using Vue, Component library to download a JSON or CSV file using Vue. <script src="​https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src=". In another tutorial, I talked about how to create a text file with JavaScript, where we used the Blob() constructor for creating a Blob object that held the data corresponding to a .txt file. The file that we created was not saved or downloaded to anywhere (e.g., to server or to user's device) since it requires further user action in order to


Component library to download a JSON or CSV file using Vue, Component library to download a JSON or CSV file using Vue import Vue from "vue"; import VueBlobJsonCsv from 'vue-blob-json-csv'; <script src="https://cdn.​jsdelivr.net/npm/vue/dist/vue.js"></script> <script src=". Blobs and object URLs exposed File downloading is a core aspect of surfing the internet. Tons of files get downloaded from the internet every day ranging from binary files (like applications, images, videos, and audios) to files in plain text. Fetching files from the server Traditionally, the file to be downloaded is first requested from a server through a client — such as […]