Heim  >  Fragen und Antworten  >  Hauptteil

node.js - nodejs 先写入文件createWriteStream,再读取文件内容createReadStream,获取不到信息

request(url).pipe(fs.createWriteStream(fpath));
                            
var bu = fs.createReadStream(fpath, {start: 0, end: 262});
dlog(bu);

我先写入文件,再读取里面的东西,但是createReadStream读取不到东西。这是为什么?是因为上面的操作是异步进行的吗?如果是,那应该如何获取呢?

迷茫迷茫2766 Tage vor494

Antworte allen(1)Ich werde antworten

  • PHP中文网

    PHP中文网2017-04-17 14:30:37

    stream是异步操作,你这种同步写法是得不到结果的,这么来:

    request(url)
        .pipe(fs.createWriteStream(fpath))
        .on('close', function() {
            var bu = fs.createReadStream(fpath, {start: 0, end: 262});
            bu.on('data', function(chunk) {
                console.log(chunk.toString());//这是结果
            });
        });

    多看文档:fs

    Antwort
    0
  • StornierenAntwort