fs模块操作文件的一些示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunycnb/article/details/84534662

最近项目操作本地文件中,使用node的fs模块比较多,主要的一些文件同步api的一些接口示例:

//上传附件
const uploadAttach = function(url) {
	let arr = url.split('\\')
	let path = attachment + '\/' + arr[arr.length - 1]
	let result = fs.readFileSync(url);
	fs.writeFileSync(path, result)
}

//同步读文件
const readdirSync = function(filePath, fun) {
	var readDir = fs.readdirSync(filePath);
	return readDir
}

//删除文件
const deleteFile = function(filePath) {
	fs.unlink(filePath, function(error) {
		if (error) {
			console.log(error);
			alert('文件删除失败')
			return false;
		}
		// alert('删除文件成功');
	})
}

//打开文件,安装node-cmd模块
const openFile = function(filePath) {
	nodeCmd.get(
		filePath,
		function(err, data, stderr) {
			console.log(err);
		}
	);
	// nodeCmd.run(filePath);
}

//新建文件夹
const mkdirApi = function(path) {
	fs.mkdirSync(path, '0777', function(err) {
		if (err) {
			alert(err)
		}
	})
}

猜你喜欢

转载自blog.csdn.net/sunycnb/article/details/84534662