Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 566 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to upload, display and save images using node.js and express

#1
I need to upload an image, and display it, as well as save it so that I don't lose it when I refresh the localhost. This needs to be done using an "Upload" button, which prompts for a file-selection.

I am using node.js and express for the server-side code.
Reply

#2
First of all, you should make an HTML form containing a [file input element](

[To see links please register here]

). You also need to [set the form's **enctype** attribute to **multipart/form-data**](

[To see links please register here]

):

<form method="post" enctype="multipart/form-data" action="/upload">
<input type="file" name="file">
<input type="submit" value="Submit">
</form>

Assuming the form is defined in **index.html** stored in a directory named **public** relative to where your script is located, you can serve it this way:

const http = require("http");
const path = require("path");
const fs = require("fs");

const express = require("express");

const app = express();
const httpServer = http.createServer(app);

const PORT = process.env.PORT || 3000;

httpServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});

// put the HTML file containing your form in a directory named "public" (relative to where this script is located)
app.get("/", express.static(path.join(__dirname, "./public")));

Once that's done, users will be able to upload files to your server via that form. But to reassemble the uploaded file in your application, you'll need to parse the request body (as multipart form data).

In **Express 3.x** you could use `express.bodyParser` middleware to handle multipart forms but as of **Express 4.x**, there's no body parser bundled with the framework. Luckily, you can choose from one of the [many available **multipart/form-data** parsers out there](

[To see links please register here]

). Here, I'll be using [multer](

[To see links please register here]

):

You need to define a route to handle form posts:

const multer = require("multer");

const handleError = (err, res) => {
res
.status(500)
.contentType("text/plain")
.end("Oops! Something went wrong!");
};

const upload = multer({
dest: "/path/to/temporary/directory/to/store/uploaded/files"
// you might also want to set some limits:

[To see links please register here]

});


app.post(
"/upload",
upload.single("file" /* name attribute of <file> element in your form */),
(req, res) => {
const tempPath = req.file.path;
const targetPath = path.join(__dirname, "./uploads/image.png");

if (path.extname(req.file.originalname).toLowerCase() === ".png") {
fs.rename(tempPath, targetPath, err => {
if (err) return handleError(err, res);

res
.status(200)
.contentType("text/plain")
.end("File uploaded!");
});
} else {
fs.unlink(tempPath, err => {
if (err) return handleError(err, res);

res
.status(403)
.contentType("text/plain")
.end("Only .png files are allowed!");
});
}
}
);

In the example above, **.png** files posted to **/upload** will be saved to **uploaded** directory relative to where the script is located.

In order to show the uploaded image, assuming you already have an HTML page containing an **img** element:

<img src="/image.png" />

you can define another route in your express app and use `res.sendFile` to serve the stored image:

app.get("/image.png", (req, res) => {
res.sendFile(path.join(__dirname, "./uploads/image.png"));
});
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through