How to upload large Files with Node.js+express
Most people don't understand the knowledge points of this article "Node.js+express how to upload large files", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "Node.js+express how to upload large files".
The details are as follows:
For large file uploads we first introduce a library called multer:
npm install --save multer
Let's start by introducing libraries into our projects:
var multer = require ('multer ')var upload = multer({ dest:'uploads/'})//files will be uploaded to this directory
Specific ways to get:
app.post ('/upload', upload.single ('logo '), function(req, res){//send json data to the routing console.dir(req.file); res.send(req.p);})
Since uploading files requires a table, let's create a form called form first:
Document Single image upload
Then we introduce the form in the server:
var fs = require ('fs ')// /html interface from, used to demonstrate uploading files app.get ('/form', function(req, res){ var form = fs.readFileSync ('./ form.html',{ encoding : "utf8"}); res.send(form);})
Upload the file after running localhost:3000/form, you can see it in the upload directory of your project:
Although the upload was successful, w I n saw that the file name was confusing and the path was fixed:
Modify path and file name:
//Change the storage path for large files var createFolder = function(folder){ try{ fs.accessSync(folder); }catch( e ){ fs.mkdirSync(folder); }};var uploadFolder = './ upload/';//set the storage folder to the current directory/upload folder createFolder(uploadFolder);//disk storage var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, uploadFolder );//it will be placed under the/upload folder in the current directory (if there is no such folder, create a new one)}, filename: function (req, file, cb) {//set the filename here cb(null, file.originalname ); }})var upload = multer({ storage: storage })
Effect:
The above is the content of this article about "Node.js +express how to upload large files". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.