将jspdf文件从Angular 6发送到NodeJS

时间:2019-05-22 06:51:47

标签: node.js angular pdf nodemailer form-data

角度代码:

我需要将jspdf pdf angular发送给nodejs。我用过XMLHttpRequest发送表单数据

var pdf = new jsPDF('l', 'pt', 'letter');

pdf.internal.scaleFactor = 2.0;
pdf.addHTML(elementToPrint, 0, 0, {
  pagesplit: true
}, function() {
  pdf.addPage()
);
});


var data = new Blob([pdf.output('blob')], {
  type: 'application/pdf'
});

var formData = new FormData();
formData.append("pdf", data, "myfile.pdf");
console.log(formData)

var request = new XMLHttpRequest();


request.open("POST", "http://localhost:3000/caseforms", true);
request.withCredentials = true;

request.send(formData);

Nodejs代码: 我正在使用formidable npm:

var form = new formidable.IncomingForm();

router.post('/', (req, res, next) => {

    form.parse(req, function(err, fields, files) {
      console.log("File received:\nName:" + files.pdf.name + "\ntype:" + files.pdf.type);
    });

    form.on('end', function(files) {

        let riffle = fs.readFileSync(this.openedFiles[0].path, 'utf-8')
        console.log(riffle)

当我得到它的控制台时,请告诉我如何解决这个问题。

%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Reso

谢谢

0 个答案:

没有答案