Easily create a nodejs server (10): handle uploaded images_node.js
WBOY
Release: 2016-05-16 16:25:40
Original
1426 people have browsed it
In this section we will implement that the user uploads a picture and displays the picture in the browser.
The external module we are going to use here is the node-formidable module developed by Felix Geisendörfer. It makes a good abstraction for parsing uploaded file data.
To install this external module, you need to execute the command under cmd:
What this module does here is convert the form submitted through HTTP POST request, which can be parsed in Node.js. All we have to do is create a new IncomingForm, which is an abstract representation of the submitted form. After that, we can use it to parse the request object and obtain the required data fields in the form.
The image files of this article’s case are stored in the /tmp folder.
Let’s solve a problem first: How to display files saved on the local hard drive in the browser?
We use the fs module to read files into the server.
Let’s add a request handler for /showURL, which is directly hard-coded to display the contents of the file /tmp/test.png to the browser. Of course, you first need to save the image to this location.
Our team will make some modifications to requestHandlers.js:
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route, handle);
After restarting the server, you can see the image saved in /tmp/test.png by visiting http://localhost:8888/show.
Okay, the last thing we want is:
Add a file upload element to the /start form
Integrate node-formidable into our upload request handler for saving uploaded images to /tmp/test.png
Embed the uploaded image into the HTML output by /uploadURL
The first item is simple. Just add a multipart/form-data encoding type to the HTML form, remove the previous text area, add a file upload component, and change the copy of the submit button to "Upload file". As shown in requestHandler.js below:
Next, we have to complete the second step. We start with server.js - remove the processing of postData and request.setEncoding (this part node-formidable will handle it itself), and instead pass the request object to How to request routing:
function route(handle, pathname, response, request) {
console.log("About to route a request for " pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response, request);
} else {
console.log("No request handler found for " pathname);
response.writeHead(404, {"Content-Type": "text/html"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;
Now the request object can be used in our upload request handler. node-formidable will handle saving the uploaded file to the local /tmp directory, and we need to
What to do is make sure the file is saved as /tmp/test.png.
Next, we put together the operations of processing file uploads and renaming, as shown in requestHandlers.js below:
var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");
function start(response) {
console.log("Request handler 'start' was called.");
var body = ''
''
''
'
During the process of uploading images, some people may encounter the following problems:
I guess the cause of this problem is caused by disk partition. To solve this problem, you need to change the default zero-time folder path of formidable to ensure that it is in the same disk partition as the target directory.
We find the upload function of requestHandlers.js and make some modifications to it:
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn