Home  >  Article  >  Web Front-end  >  How nodejs implements file upload based on express

How nodejs implements file upload based on express

亚连
亚连Original
2018-05-25 17:29:412249browse

This article mainly introduces the method of nodejs to implement file upload based on express. It analyzes the specific steps and related operation skills of nodejs based on express framework to implement file upload function based on the example. Friends in need can refer to it

The example in this article describes how nodejs implements file upload based on express. I would like to share it with you for your reference. The details are as follows:

When I was working on a personal project some time ago, I used the nodejs server to upload files. Now I will summarize this as a record.

When I upload files, I use express's multiparty. Of course, it can also be implemented using the connect-multiparty middleware, but the official does not seem to recommend the use of connect-multiparty middleware. Without further ado, let’s look at the code below.

Steps:

(1) Use express to create a project. The jade template engine is used by default, but I am still used to html, so I changed it to html template.
(2) In the project directory, install the necessary components through npm install multiparty.
(3) Modify views/index.html and add a file upload form.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>上传文件</title>
</head>
<body>
  上传文件
  <form method=&#39;post&#39;, action=&#39;/file/uploading&#39;, enctype=&#39;multipart/form-data&#39;>
    <input type="file" name="inputFile">
    <input type="submit" value="上传">
  </form>
</body>
</html>

(4) Modify routes/index.js to implement the background code for uploading pages and uploading responses.

var express = require(&#39;express&#39;);
var router = express.Router();
var multiparty = require(&#39;multiparty&#39;);
var util = require(&#39;util&#39;);
var fs = require(&#39;fs&#39;);
/* 上传页面. */
router.get(&#39;/&#39;, function(req, res, next) {
 //res.render(&#39;./views/index&#39;);
 res.sendfile(&#39;./views/index.html&#39;);
});
/* 上传 */
router.post(&#39;/file/uploading&#39;, function(req, res, next) {
  /* 生成multiparty对象,并配置上传目标路径 */
  var form = new multiparty.Form();
  /* 设置编辑 */
  form.encoding = &#39;utf-8&#39;;
  //设置文件存储路劲
  form.uploadDir = &#39;./public/files&#39;;
  //设置文件大小限制
  form.maxFilesSize = 2 * 1024 * 1024;
  // form.maxFields = 1000;  //设置所有文件的大小总和
  //上传后处理
  form.parse(req, function(err, fields, files) {
    var filesTemp = JSON.stringify(files, null, 2);
    if(err) {
      console.log(&#39;parse error:&#39; + err);
    }else {
      console.log(&#39;parse files:&#39; + filesTemp);
      var inputFile = files.inputFile[0];
      var uploadedPath = inputFile.path;
      var dstPath = &#39;./public/files&#39; + inputFile.originalFilename;
      //重命名为真实文件名
      fs.rename(uploadedPath, dstPath, function(err) {
        if(err) {
          console.log(&#39;rename error:&#39; + err);
        }else {
          console.log(&#39;rename ok&#39;);
        }
      })
    }
    res.writeHead(200, {&#39;content-type&#39;: &#39;text/plain;charset=utf-8&#39;});
    res.write(&#39;received upload:\n\n&#39;);
    res.end(util.inspect({fields: fields, files: filesTemp}))
  })
})
module.exports = router;

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

AngularJS tab bar implementation and mvc small case (graphic tutorial)

React ajax java implementation Upload pictures and preview function (graphic tutorial)

How to use ajax_Examples, ajax data processing

The above is the detailed content of How nodejs implements file upload based on express. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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