Node.js acquires post picture storage but what happens when the picture is corrupted after writing?

problem description

look at the official document directly copied official document code has been successfully written to the picture, but the picture failed to open to show damage

the environmental background of the problems and what methods you have tried

var express = require("express");
var app = express();
var fs = require("fs");

var bodyParser = require("body-parser");
var multer = require("multer");

app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: "/tmp/" }).array("image"));

app.get("/index.htm", function (req, res) {
    res.sendFile(__dirname + "/" + "index.htm");
})

app.post("/file_upload", function (req, res) {

console.log(req.files[0]);  // 

var des_file = __dirname + "/image" + req.files[0].originalname;
fs.readFile(req.files[0].path, function (err, data) {
    fs.writeFile(des_file, data, function (err) {
        if (err) {
            console.log(err);
        } else {
            response = {
                message: "File uploaded successfully",
                filename: req.files[0].originalname
            };
        }
        console.log(response);
        res.end(JSON.stringify(response));
    });
    });
})

var server = app.listen(3000, function () {

    var host = server.address().address
    var port = server.address().port

    console.log(" http://%s:%s", host, port)

})

what result do you expect? What is the error message actually seen?

I hope God can help me solve it. Thank you

clipboard.png

clipboard.png

Jun.27,2022

you can try this package https://www.npmjs.com/package...


do not understand your intention, the files uploaded through multer have been stored in the / tmp/ directory, do you want to copy another copy to _ _ dirname/image? Why not point the dest directly to _ _ dirname/image.

Menu