簡體   English   中英

將html2pdf生成的pdf發送回服務器

[英]Sending html2pdf generated pdf back to the server

我必須使用html2pdf將客戶端生成的PDF發送到服務器。 我已設法將生成的PDF轉換為base64,並希望使用axios將其發回。 這是我的客戶端代碼:

  function myFunction(){
           var element = document.getElementById('element-to-print');
           html2pdf().from(element).outputPdf().then(function(pdf) {
    //Convert to base 64
            const newpdf=btoa(pdf);
            console.log(newpdf)
            var formData = new FormData();
            formData.append("uploadedFile", newpdf);
            axios.post('/upload',formData).then(res => { console.log(res) }).catch(error => {
            console.log(error.response)
        })
    });

這是我的服務器端代碼:

app.post('/upload', fileUpload(), function(req, res) {  
  const sampleFile = req.files.uploadedFile;
  // do something with file
  res.send('File uploaded');
})

我認為問題是在客戶端,因為我在我的控制台上獲得了轉換后的pdf的base64版本,但之后我得到了錯誤:

POST http:// localhost:3000 / upload 500(內部服務器錯誤)

我該如何解決這個問題? 謝謝。

對於FORMDATA在express ,你應該使用一個中間件, Multer

你的代碼將是這樣的:

var express = require('express')
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

var app = express()

app.post('/upload', upload.single('uploadedFile'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM