Express 版本:4.14.1

在Express中,文件上传需要用到multiparty中间件,在项目目录中,通过npm install multiparty –save进行安装必要组件。

前端HTML代码,用于上传文件:

<form action="/upload" method="post" enctype="multipart/form-data">
<input id="lefile" type="file" name="filenames" multiple="multiple" />
<input type="submit" class="btn btn-primary" value="submit" />
</form>

单文件处理


后台NodeJs代码, 用于接收文件:

router.post('/upload', function(req, res) {
//生成multiparty对象,并配置上传目标路径
var form = new multiparty.Form();
//设置编辑
form.encoding = 'utf-8';
//设置文件存储路径
form.uploadDir = "E:/";
//上传完成后处理
form.parse(req, function (err, fields, files) {
var filesTmp = JSON.stringify(files, null, 2);
if (err) {
console.log('parse error: ' + err);
} else {
console.log('parse files: ' + filesTmp);
var inputFile = files['filenames'][0];
//重命名为真实文件名
fs.renameSync(inputFile.path, form.uploadDir+inputFile.originalFilename, function (err) {
if (err) {
console.log('rename error: ' + err);
} else {
console.log('rename ok');
}
});
}
res.writeHead(200, {'content-type': 'text/plain;charset=utf-8'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: filesTmp}));
});
});
module.exports = router;

多文件上传处理


//保存上传文件
files['filenames'].forEach(function(inputFile) {
//重命名为真实文件名
fs.renameSync(inputFile.path, form.uploadDir + projectName + "/" + inputFile.originalFilename,
function(err) {
if (err) {
console.log('rename error: ' + err);
} else {
console.log('rename ok');
}
});
});

获取参数值


此时,如果需要在上传文件的过程中获取post或者get参数,不能按照往常的做法,而是需要使用Multiparty中间件来获取,即在form.parse函数块中获取,如下所示:

form.parse(req, function (err, fields, files) {
projectName = fields['projectName'];
......

此处获取参数projectName的值。

参考:multiparty官方文档

在Express中使用Multiparty进行文件上传及POST、GET参数获取的更多相关文章

  1. zt对于C#中的FileUpload解决文件上传大小限制的问题设置

    对于C#中的FileUpload解决文件上传大小限制的问题设置 您可能没意识到,但对于可以使用该技术上载的文件的大小存在限制.默认情况下,使用 FileUpload 控件上载到服务器的文件最大为 4M ...

  2. 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载

    摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...

  3. 在express项目中使用formidable & multiparty实现文件上传

    安装 formidable,multiparty 模块 npm install formidable,multiparty –save -d 表单上传 <form id="addFor ...

  4. JAVA中使用FTPClient实现文件上传下载实例代码

    一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  5. JAVA中使用FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  6. action中实现对批量文件上传的封装

    如今,文件(尤其是图片)上传,在前后台的应用相当普遍,上传头像.共享资料等已成为最基本应用.我们很希望通过一个万能的封装方法来实现一劳永逸的效果. 接下来,就来为大家介绍具体实现. 首先,我们需要一个 ...

  7. springboot中使用分页,文件上传,jquery的具体步骤(持续更新)

    分页: pom.xml     加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <arti ...

  8. SpringMVC中使用 MultipartFile 进行文件上传下载及删除

    一:引入必要的包 <!--文件上传--> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fil ...

  9. Spring中使用StandardServletMultipartResolver进行文件上传

    从Spring3.1开始,Spring提供了两个MultipartResolver的实现用于处理multipart请求,分别是:CommonsMultipartResolver和StandardSer ...

随机推荐

  1. 【递推】【HDOJ】

    http://acm.hdu.edu.cn/showproblem.php?pid=2501 Tiling_easy version Time Limit: 1000/1000 MS (Java/Ot ...

  2. Go Example--switch

    package main import ( "fmt" "time" ) func main() { i := 2 fmt.Print("write ...

  3. NK实习项目配置

    1.复制eclipse和项目 2.配置tomcat6,现在只能用tomcat6 3.tomcat6插件eclipse是默认有的,只需要配置一下 http://blog.csdn.net/u014079 ...

  4. UBUNTU 安装教程

    玩过linux,只是博主觉得现在的很多服务器都是linux系统的,而自己属于那种前端也搞,后台也搞,对框架搭建也感兴趣,但是很多生产上的框架和工具都是安装在服务器上的,而且有不少大公司都要求熟悉在li ...

  5. shell excute mongo query command

    use shell command method one: #!/bin/bash ] then echo 'Please input cid' exit fi HOST= mongo ${HOST} ...

  6. WPS之word文档处理(持续更新)

    1.word文档行与行之间

  7. 普林斯顿数学指南(第一卷) (Timothy Gowers 著)

    第I部分 引论 I.1 数学是做什么的 I.2 数学的语言和语法 I.3 一些基本的数学定义 I.4 数学研究的一般目的 第II部分 现代数学的起源 II.1 从数到数系 II.2 几何学 II.3 ...

  8. Linux fdisk普通分区扩容

    买了一个orangepi 然后用7.4GB的内存卡,写入了一个lubuntu镜像,用去3.6GB还有3.8GB没有用,因为要编译mt7601u进ubuntu中,需要用到内核文件 但是内核压缩包1.2G ...

  9. [c/c++]fopen用法及参数介绍

    函数定义: FILE * fopen ( const char * filename, const char * mode ); 参数mode: "r" read: 为输入操作打开 ...

  10. Spring 4 中重定向RedirectAttributes的使用

    RedirectAttributes 的使用 @RequestMapping(value = "/redirecttest", produces = "applicati ...