Use node.js to accept the base64 pictures sent from the front end, how to save them in the background?

        //url
        let base64 = imgSrc.replace(/^data:image\/\w+;base64,/, "")
        //buffer
        let dataBuffer = new Buffer(base64, "base64")
        //
        let path = "static/upload/image"+".jpg"
        //
        fs.writeFile(path,dataBuffer,(err) => {
            if(err) {
                console.log(err)
            }else {
                console.log("")
            }
        })

what I do:
1. First, drop the url Filter of the base64 image to the data:image. in front of it. String
2. Convert the pictures of Filter to binary
3. Save to background with fs

but the saved picture cannot be parsed (as shown in the picture), so how can I get the picture at the front end?
in this way, the image obtained by the front end cannot be displayed because it cannot be parsed. Is it because I saved it in the wrong way? what should I do?

clipboard.png

Mar.11,2021

link


1. If you want to save it in base64, you don't need to convert it to binary, just save the base string.
2. Because if you want to upload a picture, why do you encode it and then upload it and then decode it? Oh, this is very naughty.
such as 2. Only need to encode and decode, not including converting to binary, so your operation will not get the final jpeg picture


has found the reason why the picture cannot be parsed. Base64 encoding. After receiving the post value using express, the "+" sign in the base64 encoding string is replaced with a space, resulting in an coding error. Img.src = base64Data; directly hangs the nodejs service. If you have a similar problem, please console.log (base64Data); see if the string has spaces.

solution:

replace the space back to the "+" sign

var base64Data = imgData.replace (/ sambig, "+");

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35e77-2bffc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35e77-2bffc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?