本文介绍了使用node.js(http.get)读取远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取远程文件的最佳方法是什么?我想获取整个文件(而不是大块).

Whats the best way to read a remote file? I want to get the whole file (not chunks).

我从以下示例开始

var get = http.get(options).on('response', function (response) {
    response.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

我想将文件解析为csv,但是为此,我需要整个文件而不是分块数据.

I want to parse the file as csv, however for this I need the whole file rather than chunked data.

推荐答案

为此,我会使用请求:

request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))

或者,如果您不需要先保存到文件中,而只需要将CSV读取到内存中,则可以执行以下操作:

Or if you don't need to save to a file first, and you just need to read the CSV into memory, you can do the following:

var request = require('request');
request.get('http://www.whatever.com/my.csv', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var csv = body;
        // Continue with your processing here.
    }
});

这篇关于使用node.js(http.get)读取远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:13