npm multer 文件上传

Express app 范本就不写了,仅记录一下上传部分的代码。

const fs = require('fs');
const express = require('express');
const multer = require('multer'); const multer_dest = multer({dest: 'public/uploads/'}); const async_route_wrap = fn => {
return function wrap(...args) {
const ret = fn(...args);
const next = args[args.length - 1];
return Promise.resolve(ret).catch(next);
};
}; const index = async(req, res) => { res.send(`<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file1"/>
<button type="submit">upload</button>
</form`);
}; const upload = async(req, res) => { if (!req.files) return res.json({status: 'failed.'}); const count = req.files.length;
for (let i = 0; i < count; i++) { const {originalname, size, path: upload_path} = req.files[i];
const save_path = `./public/uploads/${originalname}`; console.log(`Name: ${originalname}, Size: ${size}`); fs.renameSync(upload_path, save_path); /*
fs.readFile(file_path, (err, data) => {
fs.writeFile(new_path, data, (err) => {
if (err) {
console.error(err);
throw err;
} else {
if (i + 1 === count) {
res.json({status: 'success'});
}
}
});
});
*/
} res.json({
status: 'success'
});
}; module.exports = () => {
const router = express.Router(); router.get('/', async_route_wrap(index));
router.post('/upload', multer_dest.any(), async_route_wrap(upload)); return router;
};

看到注释的那段读写文件的code了,其实multer已经把文件保存在multer_dest.dest的文件夹下面了,在同一个文件夹下,只需要rename就行了,不需要再读写了。

Express multer 文件上传的更多相关文章

  1. express + multer 文件上传入门

    写在前面的 在web开发中,我们经常会遇到图片上传的功能,接下来我们就在express4.15.0框架中利用multer1.3.0模块来实现图片上传 开始敲代码 首先利用express-generat ...

  2. node+express实现文件上传功能

    在进行node web开发时,我们可能经常遇到上传文件的问题,这一块如果我们没有经验,可能会遇到很多坑,下面我将跟大家分享一下,实现文件上传的一些方式. 一.node+express文件上传的常用方式 ...

  3. node express formidable 文件上传后修改文件名

    //我是用php的思想来学习nodejs var express = require('express'); var router = express.Router(); var fs = requi ...

  4. Vue+axios+Node+express实现文件上传(用户头像上传)

    Vue 页面的代码 <label for='my_file' class="theme-color"> <mu-icon left value="bac ...

  5. koa2的文件上传

    使用koa2搭建文件上传服务,后端代码 const os = require('os'); const path = require('path'); const koaBody = require( ...

  6. Nodejs进阶:基于express+multer的文件上传

    关于作者 程序猿小卡,前腾讯IMWEB团队成员,阿里云栖社区专家博主.欢迎加入 Express前端交流群(197339705). 正在填坑:<Nodejs学习笔记> / <Expre ...

  7. node.js中 express + multer 处理文件上传

    multer中间件,可以很方便的结合express处理用户表单上传的文件. 一.安装multer npm install multer 二.处理单个文件上传 const express = requi ...

  8. express文件上传中间件Multer详解

    express文件上传中间件Multer详解 转载自:https://www.cnblogs.com/chengdabelief/p/6580874.html   Express默认并不处理HTTP请 ...

  9. Express文件上传之Multer

    Express文件上传之Multer Multer是一个nodejs中间件,用来处理http提交multipart/form-data,也就是文件上传.它是在busboy的基础上开发的. 在我看来,M ...

随机推荐

  1. compare across commits online

    https://gist.github.com/nevik/5689882 Examples: https://github.com/octocat/Spoon-Knife/compare/ed122 ...

  2. LC 894. All Possible Full Binary Trees

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  3. python的XML解析

    http://www.jb51.net/article/63780.htm http://www.runoob.com/python/python-xml.html http://kb.cnblogs ...

  4. openstack部署keystone

    环境: 免密钥,域名解析 cat /etc/hosts 192.168.42.120 controller 192.168.42.121 compute 192.168.42.122 storage ...

  5. [笔记] Delphi使用DUnitX做单元测试的简单例子

    Delphi XE 提供了对DUnitX的支持,记录一个最简例子. 首先创建项目A,然后创建单元untCalc,代码如下: unit untCalc; interface type TCalc = c ...

  6. 系统启动热键(Boot Hotkey)

    1.HP ENVY 13 F1 --> 系统信息F2 --> 系统检测F9 --> 启动设备选项F10 --> 设置BIOSF11 --> 系统恢复ENTER --> ...

  7. 抓包工具chlers的使用

    1,下载chlers破解版:https://zhubangbang.com/charles-crack-version-free-download-and-install-tutorial.html ...

  8. 机器学习:gensim之Word2Vec 详解

    一 前言 Word2Vec是同上一篇提及的PageRank一样,都是Google的工程师和机器学习专家所提出的的:在学习这些算法.模型的时候,最好优先去看Google提出者的原汁Paper和Proje ...

  9. 仿flash运动框架

    github地址: [https://github.com/linxd5/pictureShow] PS: 新建一个github项目很简单,只要new一个repo,后面按照提示做就可以了~ 项目思路: ...

  10. java线程中start和run的区别

    public class Test1 extends Thread { @Override public void run() { while (true) { System.out.println( ...