将html2pdf生成的pdf发送回服务器

编程入门 行业动态 更新时间:2024-10-27 20:29:33
本文介绍了将html2pdf生成的pdf发送回服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须将使用 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发送回服务器

本文发布于:2023-11-11 11:10:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   html2pdf   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!