我正在从节点应用程序中的客户端应用程序接收分块文件。不同的块来自不同的请求。然后,我通过使用带填充的写入流写入单个文件,将这些块组装成服务器上的整个文件。
我遇到的问题是,当我写入第一个块时,我需要创建文件,而对于所有后续块,我只需要修改该文件。为了实现这一点,我使用fs.createWriteStream(filePath)- 这将在每次调用它时重写该文件,因此我需要传递特殊标志:r+。这个标志的问题是它只适用于已经存在的文件,如果找不到它会抛出错误。
所以我想知道是否有任何标志会强制 fs 创建一个文件(如果它不存在)或更新现有文件而不显式调用 fs.exists
这是我目前想到的:
//check if file exists
fs.exists(filePath, function(exists) {
//if it does exist, then modify it
if (exists) {
flags = 'r+';
//else create a new one
} else {
flags = 'w';
}
var ws = fs.createWriteStream(filePath, {
start: chunkInfo.chunkNumber * chunkInfo.chunkSize, //set chunk padding
flags: flags
});
file.pipe(ws); //pipe the file read stream to write stream
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1087 次 |
| 最近记录: |