简体   繁体   English

如何将大型 base64 图像发送到 nodejs 服务器

[英]how to send a large base64 image to nodejs server

im using this function to convert html div to pdf, but i want to store it on the server so i can then attach it to an email and send it using nodmailer, now my problem is that i dont know how to store it on the server, im using this function to convert html div to pdf, but i want to store it on the server so i can then attach it to an email and send it using nodmailer, now my problem is that i dont know how to store it on the server ,

frontEnd is js backEnd is nodejs前端是js后端是nodejs

all kind of help is appreciated so much非常感谢各种帮助

FRONT END JS前端JS

    html2canvas($(".html-content")[0]).then(function (canvas) {
        var imgData = canvas.toDataURL("image/jpeg", 1.0);
   }
        
        $.ajax({
           type: "post",
           url: "/receive",
           data: imgData,
        })
    
    });

BACKEND nodejs后端nodejs

const express = require("express");
const router = require("express").Router();
router.use(express.json({limit: '50mb'}));
router.use(express.urlencoded({limit: '50mb', extended: true, parameterLimit: 50000}));


router.post('/receive', (req, res) => {
    
    console.log("my req body: ", req.body)
})

CONSOLE LOG控制台日志

PayloadTooLargeError: request entity too large
    at readStream (C:\Users\user\Desktop\project\node_modules\raw-body\index.js:155:17)
    at getRawBody (C:\Users\user\Desktop\project\node_modules\raw-body\index.js:108:12)
    at read (C:\Users\user\Desktop\project\node_modules\body-parser\lib\read.js:77:3)
    at urlencodedParser (C:\Users\user\Desktop\project\node_modules\body-parser\lib\types\urlencoded.js:116:5)
    at Layer.handle [as handle_request] (C:\Users\user\Desktop\project\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\user\Desktop\project\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\user\Desktop\project\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\user\Desktop\project\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\user\Desktop\project\node_modules\express\lib\router\index.js:275:10)
    at jsonParser (C:\Users\user\Desktop\project\node_modules\body-parser\lib\types\json.js:119:7)

EDIT 1编辑 1

setting a contentType: "image/jpeg" , on ajax, gets rid of payload too large error, but nothing is displayed on the back end,设置contentType: "image/jpeg" ,在 ajax 上,去掉 payload too large 错误,但后端没有显示,

console.log("reqbody: " +  req.body)

output output

reqbody: [object Object]

If you want to do it the way you are doing it (generating a pdf on the client and then sending it to the server to mail)如果您想按照自己的方式进行操作(在客户端生成 pdf,然后将其发送到服务器以邮寄)

according to https://github.com/parallax/jsPDF/issues/3082根据https://github.com/parallax/jsPDF/issues/3082

you can put your pdf in a variable like this pdf.output()你可以把你的 pdf 放在像这样的变量中pdf.output()

but then you should not put it in the query - put it in the payload但是你不应该把它放在查询中 - 把它放在有效负载中

something like就像是

$.ajax({
  type: "POST",
  url: url,
  data: pdf.output(),
  // dataType: dataType // don't know if you need this too so it doesn't think it's json
});

and then to receive it on the server.然后在服务器上接收它。 It won't be in the query string anymore;它不再出现在查询字符串中; I think you can use express's "body parser middleware" to get the playload.我认为您可以使用 express 的“正文解析器中间件”来获取播放负载。


However, are you sure that you want to generate the pdf on the client and send it back to the server?但是,您确定要在客户端生成 pdf 并将其发送回服务器吗? A user could intercept the ajax request and send a different PDF... which might not be an issue for some applications, but could be bad for others.用户可以拦截 ajax 请求并发送不同的 PDF... 这对于某些应用程序可能不是问题,但对其他应用程序可能是坏事。

For most things I think it would be better to generate the pdf on the server and then send it to the browser to show (using an ajax request that sends the pdf the opposite way from what you have) and mail it at the same time.对于大多数事情,我认为最好在服务器上生成 pdf,然后将其发送到浏览器以显示(使用 ajax 请求发送 Z437175BA4191210EE004E1D937494D09 的请求,同时从相反的方式发送邮件。

You encoded the canvas as a jpeg using canvas.toDataURL("image/jpeg", 1.0);您使用canvas.toDataURL("image/jpeg", 1.0); , but I would suggest encoding using canvas.toBlob and setting an actual contentType of image/jpeg in the ajax call (instead of using the default , which is application/x-www-form-urlencoded; charset=UTF-8 ) and then receive the raw body using express.raw({ type: '*/*', limit: '50mb'}) (or you can set a more restrictive type to just image/jpeg or image/* or whatever is most appropriate for your application.) ,但我建议使用canvas.toBlob进行编码,并在 ajax 调用中设置image/jpeg的实际contentType (而不是使用默认值,即application/x-www-form-urlencoded; charset=UTF-8 ),然后使用express.raw({ type: '*/*', limit: '50mb'})接收原始正文(或者您可以将更严格的类型设置为仅image/jpegimage/*或任何最适合您的应用。)

In the client:在客户端:

$.ajax({
  type: "post",
  url: "/receive",
  data: imgData,
  contentType: "image/jpeg"
})

And in your server:在您的服务器中:

app.use(express.raw({ type: '*/*', limit: '50mb' }));

The incoming payload will be in req.body as a Buffer .传入的有效负载将在req.body作为Buffer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM