Skip to content Skip to sidebar Skip to footer

How To Write To Csv File In Javascript

I have a script (using PhantomJS) that tests how long it takes to load a webpage. What I am trying to figure out is how to write the result of time taken to load the page to a .csv

Solution 1:

You can use the fs module with the write(path, content, mode) method in append mode.

var fs = require('fs');
fs.write(filepath, content, 'a');

where filepath is the file path as a string and content is a string containing your CSV line.

Something like:

address+";"+(newDate()).getTime()+";"+t

Solution 2:

If you have control over the Jenkins environment, you can use one of the browser specific methods of triggering a download like suggested in This Question

functiondownload(strData, strFileName, strMimeType) {
    var D = document,
        A = arguments,
        a = D.createElement("a"),
        d = A[0],
        n = A[1],
        t = A[2] || "text/plain";

    //build download link:
    a.href = "data:" + strMimeType + "charset=utf-8," + escape(strData);

    if (window.MSBlobBuilder) { // IE10var bb = newMSBlobBuilder();
        bb.append(strData);
        return navigator.msSaveBlob(bb, strFileName);
    } /* end if(window.MSBlobBuilder) */if ('download'in a) { //FF20, CH19
        a.setAttribute("download", n);
        a.innerHTML = "downloading...";
        D.body.appendChild(a);
        setTimeout(function() {
            var e = D.createEvent("MouseEvents");
            e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            a.dispatchEvent(e);
            D.body.removeChild(a);
        }, 66);
        returntrue;
    }; /* end if('download' in a) *///do iframe dataURL download: (older W3)var f = D.createElement("iframe");
    D.body.appendChild(f);
    f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64" : "") + "," + (window.btoa ? window.btoa : escape)(strData);
    setTimeout(function() {
        D.body.removeChild(f);
    }, 333);
    returntrue;
}

Maybe you can use this URL SCM Plugin to grab the download. Or use Selenium to automate some things and grab the download file

Post a Comment for "How To Write To Csv File In Javascript"