How i can send -tabulator excel- object to server side (node.js)?

て烟熏妆下的殇ゞ 提交于 2020-01-11 07:36:23

问题


I can export tabulator table to excel with success and store it as file on local computer. However i don't know how to send excel object to server.

How i can convert tabulator excel to base64 before save to local computer and send it to server side?

I already do the same thing with jsPdf and send pdf using: doc.output('datauristring').

The success scenario should be: 1. Produce excel object (on client side) 2. Convert it to base64 Uri (on client side) 3. Send it to server side (node.js) 4. Convert it again to file (use of fs. to write on local path)

Any help will be appreciated! Thanks in advance!

UPDATE

Let me give you the working scenario of sending pdf to server. The same thing must be done with xlsx. I've tried to store tabulator object to variable before save to local client computer, but with no success.


    table.download("xlsx", "test.xlsx", {sheetName:"testName"});

WORKING SCENARIO WITH PDF

  1. CREATE PDF ON CLIENT END
function canvasRun(){            
        // SYSTEM DATE TO ADD AS NAME
        var today = new Date();
        var dd = String(today.getDate()).padStart(2, '0');
        var mm = String(today.getMonth() + 1).padStart(2, '0');
        var yyyy = today.getFullYear();
        today = dd+ '-' + mm + '-' +yyyy;
                                             html2canvas(document.getElementById("grid")).then(function(canvas) {

        //var doc = new jsPDF("l","mm","a4",true); //landscape, millimetre, A4, pdfCompression
        var width = doc.internal.pageSize.getWidth();
        var height = doc.internal.pageSize.getHeight();
        var img = canvas.toDataURL('image/png');

        doc.text("Test1 : "+ today, 110, 15);  // TEXT AT THE CENTER OF REPORT
        doc.text('Test2 : '+ grandTotal +' mm', 200, 15);  // TEXT AT THE CENTER OF REPORT
        doc.addImage(logo, 'PNG', 10, 1, 38, 20,'FAST');// MARGINS LEFT,TOP,WIDTH,HEIGHT

        doc.addImage(img, 'JPEG', 10, 20,width-20, height-20,'','FAST');
        doc.save('testReport-'+today+'.pdf'); // DOWNLOAD TO LOCAL COMPUTER

        });//HTMLCanvas end
        flagGeneratedPdf=1;
        }

  1. SEND PDF TO SERVER SIDE

function sendToServer(){

// CONVERT PDF FILE TO BASE64 STREAM URI
var convPDF= doc.output('datauristring'); 

// LOAD URI PDF TO OBJECT
var obj = new Object;
obj.attachment=convPDF;

.....
.....

//method to send data to server        
}

  1. SERVER ACCEPTS OBJECT AND PROCESS IT

    function dataFromClient(attachment){

    // CONVERT INCOMING URi TO base64 FILE AND REMOVE MIME TYPE FROM HEADER
    let base64String = attachment;
    let base64pdf = base64String.split(';base64,').pop();

    // GENERATE PDF LOCAL SERVER FILE, IF EXIST REPLACE...
    fs.writeFile("test.pdf", base64pdf,{encoding:'base64'}, (err) => {
    if (err) console.log(err);
    console.log("Successfully Written to File.");});
    }

[SOLVED]

I figured out the solution using this post

  1. Intercept Tabulator's blob object
downloadReady:function(fileContents, blob){

getBase64(blob);

return blob;
}, 
  1. Convert blob to base64 dataUri with this method
function getBase64(file) {
   var reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = function () {
    // console.log(reader.result);
     var xlsxUriString =reader.result;
   };
   reader.onerror = function (error) {
     console.log('Error: ', error);
   };
}
  1. Send xlsxUriString to server side (node.js)

  2. Convert base64 to file and save it to local folder

 let base64StringXlsx = attachment2;
 let base64Xlsx = base64StringXlsx.split(';base64,').pop();

 fs.writeFile("D:/testFolder/test.xlsx", base64Xlsx,{encoding:'base64'}, (err) => {
             if (err) console.log(err);
             console.log("Successfully Written to File xlsx.");
});

来源:https://stackoverflow.com/questions/58271078/how-i-can-send-tabulator-excel-object-to-server-side-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!