node.js express框架实现文件上传与下载功能实例详(2)

const path = require('path'); const fs = require('fs'); /** * 创建目录 * @param dirpath */ exports.mkdirSync = function (dirpath){ if (!fs.existsSync(dirpath)) { var pathtmp; dirpath.split(path.sep).forEach(function(dirname) { if (pathtmp) { pathtmp = path.join(pathtmp, dirname); } else { pathtmp = dirname; } if (!fs.existsSync(pathtmp)) { fs.mkdirSync(pathtmp); } }); } }; //删除所有的文件(将所有文件夹置空) exports.emptyDir = function(dirpath){ var self = this; //读取该文件夹 var files = fs.readdirSync(dirpath); files.forEach(function(file){ var filePath = dirpath + 'https://www.jb51.net/' + file; var stats = fs.statSync(filePath); if(stats.isDirectory()){ self.emptyDir(filePath); }else{ fs.unlinkSync(filePath); } }); };

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/0071a28de956f1ac92e29d9b37ec8b61.html