我必须将使用 html2pdf 在客户端生成的PDF发送到服务器.我设法将生成的PDF转换为base64,并想使用 axios 发回.这是我的客户端代码:
I have to send a PDF generated on the client side using the html2pdf to a server. I have managed to convert the generated PDF to base64 and want to send it back using axios. Here is my client side code:
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) }) });这是我的服务器端代码:
Here is my server side code:
app.post('/upload', fileUpload(), function(req, res) { const sampleFile = req.files.uploadedFile; // do something with file res.send('File uploaded'); })我认为问题出在客户端,因为我在控制台上获得了转换后的pdf的base64版本,但此后我得到了错误:
I think the problem is in client side as I get the base64 version of my converted pdf on my console but after that I get the error:
POST localhost:3000/upload 500(内部服务器错误)
POST localhost:3000/upload 500 (Internal Server Error)
如何解决此问题?谢谢.
How do I solve this problem? Thanks.
推荐答案对于 express 中的FormData,您应该使用马尔特
For the FormData in express you should use a middleware such 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 })更多推荐
将html2pdf生成的pdf发送回服务器
发布评论