从NodeJS流式传输文件

时间:2014-10-10 18:03:19

标签: node.js weed-fs

我正在使用weed-fs来存储文件并尝试使用以下代码将文件流式传输到客户端

var fileName = [__dirname, '/uploads/', req.params.id, ".png"].join('');

try {
    var writeStream = fs.createWriteStream(fileName);
    weedfs.read(req.params.id, writeStream);
    writeStream.on("close", function () {
        var readStream = fs.createReadStream(fileName);
        response.pipe(readStream);
        response.on('error', function (err) {
            readStream.end();
        });
        response.on('end', function (err) {
            removeFile(fileName);
        });
    });

} catch (e) {
    res.send(404);
}

但我收到错误Error: connect ECONNREFUSED

如果我查看weedfs读取方法,我会在下面找到代码

read: function(fid, stream, cb) {
    var ins = this;
    if ((typeof stream == "function") && !cb) {
        cb     = stream;
        stream = false;
    }

    this.find(fid, function(pub) {
        if (pub[0]) {
            if (stream) {
                ins.http(pub[0]).pipe(stream);
        ...... 

我做错了什么。请帮我解决这个问题。

0 个答案:

没有答案