在Node.js中,我们可以使用formidable模块来轻松地实现文件上传功能,代码如下:
var Q = require('q'); var util = require('util'); var fs = require('fs'); var path = require('path'); var moment = require('moment'); var formidable = require('formidable'); var imageUpload = function (){ }; imageUpload.prototype.useFormParseCallback = function(req){ var deferred = Q.defer(); var form = new formidable.IncomingForm(); form.parse(req, deferred.makeNodeResolver()); return deferred.promise; }; // 确保目录存在 // 如果指定的目录不存在则同步创建(多层次) imageUpload.prototype.ensureUploadPathExist = function(uploadPath, mode){ if (!fs.existsSync(uploadPath)){ var pathtmp; uploadPath.split(path.sep).forEach(function(dirname) { if (pathtmp) { pathtmp = path.join(pathtmp, dirname); } else { pathtmp = dirname; } if (!fs.existsSync(pathtmp)) { if (!fs.mkdirSync(pathtmp, mode)) { return false; } } }); } return true; }; imageUpload.prototype.uploadImage = function(req){ var pathName = '/uploadImgs/'; var uploadPath = path.join(__dirname, '../../public', pathName); this.ensureUploadPathExist(uploadPath); return this.useFormParseCallback(req).then(function(files){ var file = files[1].imagefile; var fileType = file.type.split('/')[1]; var newFileName = 'upload_' + moment().format('x') + Math.random().toString().substr(2, 10) + '.' + fileType; fs.renameSync(file.path, uploadPath + newFileName); return pathName + newFileName; }); }; module.exports = imageUpload;
上述代码中使用了模块q来处理Node.js中的回调处理,有关如何使用q可以自己上百度搜索下,这里不再赘述。函数ensureUploadPathExist()可以确保上传文件的目标目录是存在的,如果不存在它会逐级创建。文件被上传在/public/uploadImgs/目录下,并且按照指定的格式进行了重命名以防止文件被覆盖,有问题的地方在fs.renameSync()函数的调用。正常情况下如果上传的目录在本地,执行fs.renameSync()函数重命名文件是不会有问题的,但如果是跨分区重命名,例如在Linux中使用了NFS或者共享目录,则此处会报跨分区重命名的权限错误。解决办法是将fs.renameSync()函数替换为下面的代码:
var readStream = fs.createReadStream(file.path); var writeStream = fs.createWriteStream(uploadPath + newFileName); var deferred = Q.defer(); util.pump(readStream, writeStream, deferred.makeNodeResolver()); return deferred.promise.then(function() { fs.unlinkSync(file.path); return pathName + newFileName; });
使用上述代码既可以实现本地文件的上传,也可以实现跨分区的文件上传。
本文转自Jaxu博客园博客,原文链接:http://www.cnblogs.com/jaxu/p/5100636.html,如需转载请自行联系原作者