The node file upload function is simple to implement code, and the node file upload code

Source: Internet
Author: User

The node file upload function is simple to implement code, and the node file upload code

Find a lot of File Upload related modules, and finally select the more common, and express is recommended to use multer for file upload, attached GitHub address

1. Start

The first step is to install the module.

npm install multer --save

This is a simple example. Because the post method is used to submit data for file uploads, the uploaded single or multiple files will be added to the request object as a body. We can use req. file or req. files.

Taking single file upload as an example, req. file returns an object:

{"Fieldname": "avatar", # name of the input File Uploaded By the front-end "originalname": "Wx. php ", # local file name" encoding ":" 7bit ", # file encoding type" mimetype ":" text/php ", # file type" destination ":" uploads /", # upload the root directory "filename": "1497286037422Wx. php ", # Name of the uploaded file" path ":" uploads/1497286037422Wx. php ", # file path" size ": 18174 # file size}

The key value of this object is fixed, and the velue value is generated based on the configuration to implement the relevant logic.

2. Implementation

The implementation is divided into two parts: front-end and back-end

Front end

The front-end is a common method, and form submission

<Form action = "/test/upload" method = "post" enctype = "multipart/form-data"> <input type = "file" name = "avatar"> <input type = "submit" name = "submit"> </form>

Remember, enctype = "multipart/form-data" must be added, otherwise the background will not receive the file.

Backend

First, create a configuration file, upload. js

// Upload. jsvar multer = require ('multer '); # introduce the module var storage = multer. diskStorage ({destination: function (req, file, cb) {cb (null, 'uploads/')}, filename: function (req, file, cb) {cb (null, date. now () + file. originalname) }}) var upload = multer ({storage: storage}) module. exports = upload;

diskStorageThe method is equivalent to creating a disk storage engine, Configuration File Upload path, file name, etc., with higher controllability.

Destination # Set the File Upload path filename # rename the file

Create a new route Receiving file, file. js

// File. js var express = require ('express '); var router = express. router (); // introduce the configuration file var upload = require ('.. /config/upload '); router. post ('/upload', upload. single ('Avatar '), function (req, res, next) {res. send (req. file) ;}); module. exports = router;

In file. js, the upload. single () method indicates that a single file is accepted.

Upload. single (fname); // receives a single file upload. array (fname [, maxCount]) // receives multiple files, maxCount indicates the maximum number of received files

Fname is the name value of the front-end <input type = "file" name = "fname">

This is the basic file upload method. Of course there are many configuration parameters and other settings. For details, refer to the GitHub instructions. The address is at the beginning and can be checked by the publisher.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.