2014-10-08 25 views
0

請看下面的示例代碼。是否可以將一個流從一個功能轉發到另一個功能?

如您所見,它使用busboy解析傳入的表單數據並將傳入文件寫入光盤。 讓我們假設這些只是圖像文件,因爲我的示例代碼使用imgur.com。 (一個內容長度報頭不需要被髮送) 的imgurUpload()函數使得使用node-form-data

是它在某種程度上可以額外流中的圖像文件,而不需要緩衝它們完整,給imgur。 COM? (到imgurUpload()函數和節點形式的數據中使用它?)

服務器/聽衆:

var http = require('http'), 
    Busboy = require('busboy'), 
    FormData = require('form-data'), 
    fs = require('fs') 

http.createServer(function(req, res) { 
    if (req.method === 'POST') { 
    var busboy = new Busboy({ 
     headers: req.headers 
    }) 
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { 
     //pipe the stream to disc 
     file.pipe(fs.createWriteStream('1st-' + filename)) 
     //pipe the stream a 2nd time 
     file.pipe(fs.createWriteStream('2nd-' + filename)) 

     /* how to connect things together? */ 
    }) 
    busboy.on('finish', function() { 
     res.writeHead(200, { 
     'Connection': 'close' 
     }) 
     res.end("upload complete") 
    }) 
    return req.pipe(busboy) 
    } else if (req.method === 'GET') { 
    var stream = fs.createReadStream(__dirname + '/index.html') 
    stream.pipe(res) 
    } 
}).listen(80, function() { 
    console.log('listening for requests') 
}) 

HTML測試形式(的index.html)

<!doctype html> 
<head></head> 
<body> 
    <form action="/upload" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file"> 
    <input type="submit" value="submit"> 
    </form> 
</body> 
</html> 

樣本功能該提交的圖像文件imgur.com:

function imgurUpload(stream) { 

    var form = new FormData() 
    //form.append('Filedata', fs.createReadStream('test.jpg')) 

    form.append('Filedata', /* how to connect things together? */X) 
    form.submit('http://imgur.com/upload', function(err, res) { 
    if (err) throw err 

    var body = '' 
    res.on('data', function(chunk) { body += chunk }) 
    res.on('end', function() { console.log('http://imgur.com/' + JSON.parse(body).data.hash) }) 
    }) 
} 

更新(關於MSCDEX答案)

_stream_readable.js:748 
    throw new Error('Cannot switch to old mode now.'); 
     ^
Error: Cannot switch to old mode now. 
    at emitDataEvents (_stream_readable.js:748:11) 
    at FileStream.Readable.pause (_stream_readable.js:739:3) 
    at Function.DelayedStream.create (\path\node_modules\form-data\node_modules\combined-stream\node_modules\delayed-stream\lib\delayed_stream.js:35:12) 
    at FormData.CombinedStream.append (\path\node_modules\form-data\node_modules\combined-stream\lib\combined_stream.js:45:30) 
    at FormData.append (\path\node_modules\form-data\lib\form_data.js:43:3) 
    at imgurUpload (\path\app.js:54:8) 
    at Busboy.<anonymous> (\path\app.js:21:4) 
    at Busboy.emit (events.js:106:17) 
    at Busboy.emit (\path\node_modules\busboy\lib\main.js:31:35) 
    at PartStream.<anonymous> (\path\node_modules\busboy\lib\types\multipart.js:205:13) 

回答

1

您可以添加可讀流,如node-form-data的自述文件中所示。所以這個:

form.append('Filedata', stream); 

應該工作得很好。

然後在你的file事件處理程序:

imgurUpload(file); 
+0

我已經嘗試過這種方式,但遇到了一個錯誤(更新我的問題,並添加了錯誤日誌)。我之前沒有注意到「現在不能切換到舊模式」。我查了一下 ,發現這個帖子:[鏈接](https://stackoverflow.com/a/20181116/4121003) - 所以它應該工作,但它不是因爲一個模塊仍然使用舊的流接口導致問題? – jaspis 2014-10-08 20:33:21

+0

聽起來像是'form-data'和/或'combined-stream'模塊中的一個bug。 – mscdex 2014-10-08 21:42:58

相關問題