reymesson1 / septimoejercicio

1 stars 0 forks source link

Download csv #67

Closed reymesson1 closed 5 years ago

reymesson1 commented 5 years ago

exports.getMasterCSV = async(req,res)=>{

var master = await Master.find({})

var arr = [];

var second = [];

for(var x=0;x<master.length;x++){

second = []

second.push("'"+master[x].id)
second.push(master[x].date)
second.push(master[x].name)
second.push(master[x].project)
second.push(master[x].status)

arr.push(second)

}

res.send(arr); }

reymesson1 commented 5 years ago

app.get('/mastercsv', masterController.getMasterCSV)

reymesson1 commented 5 years ago

masterAPICSV:[]

reymesson1 commented 5 years ago

fetch(API_URL+'/mastercsv',{headers: API_HEADERS}) .then((response)=>response.json()) .then((responseData)=>{
this.setState({

              masterAPICSV: responseData
          })
      })
reymesson1 commented 5 years ago

downloadCSV(){

    console.log(this.state.masterAPICSV)

    //const rows = [["name1", "city1", "some other info"], ["name2", "city2", "more info"]];        
    const rows = this.state.masterAPICSV
    let csvContent = "data:text/csv;charset=utf-8,";
    rows.forEach(function(rowArray){
       let row = rowArray.join(",");
       csvContent += row + "\r\n";
    }); 

    var encodedUri = encodeURI(csvContent);
    window.open(encodedUri);

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "my_data.csv");
    document.body.appendChild(link); // Required for FF

    link.click(); // This will download the data file named "my_data.csv".

}
reymesson1 commented 5 years ago

<SplitButton bsStyle={'default'} title={'Add Master'} key={'1'} id={split-button-basic-${'1'}} onClick={this.open.bind(this)}

Download CSV

reymesson1 commented 5 years ago

const SplitButton = ReactBootstrap.SplitButton;