node+express4+multiparty实现简单文件上传
文件上传
var fs = require('fs'); var express = require('express'); var multiparty = require('multiparty'); var app = express(); /*文件上传*/ app.post('/uploadimg', function(req, res, next){ //生成multiparty对象,并配置上传目标路径 var form = new multiparty.Form({uploadDir: './uploads'}); //上传完成后处理 form.parse(req, function(err, fields, files){ var inputFile = files.file[0]; var uploadedPath = inputFile.path; var dstPath = './uploads/' + inputFile.originalFilename; fs.rename(uploadedPath, dstPath, function(err) { if(err){ console.log('rename error: ' + err); } else { console.log('rename ok'); } }); files.file.path = dstPath; var data = files; res.send(data); }); }); app.listen(3000,function(){ console.log("start:http://localhost:3000/uploadimg") })
本文来自博客园,作者:_zhiqiu,转载请注明原文链接:https://www.cnblogs.com/guojikun/p/6679385.html