write在您从
tempFile写入流中收到
'open'事件之前,您不应该调用写入流。除非您看到该事件,否则该文件将不存在。
为了您的功能:
function download(url, tempFilepath, filepath, callback) { var tempFile = fs.createWriteStream(tempFilepath); tempFile.on('open', function(fd) { http.request(url, function(res) { res.on('data', function(chunk) { tempFile.write(chunk); }).on('end', function() { tempFile.end(); fs.renameSync(tempFile.path, filepath); return callback(filepath); }); }); });}为了您的测试:
var ws = fs.createWriteStream('anypath');ws.on('open', function(fd) { console.log(fs.existsSync('anypath')); console.log(fs.existsSync('anypath')); console.log(fs.existsSync('anypath'));});


