2015-06-21 13 views
-1

所以我有一種奇怪的錯誤。如果我做以下操作:節點流添加標頭錯誤地發送文件

res.header('content-disposition', 'filename=' + mediaObject.file.originalname); 

我可以下載該文件。

但是隻要我一個額外的行添加到標題中的文件下載錯誤,例如:

res.header('content-disposition', 'filename=' + mediaObject.file.originalname,"Content-Length: " + mediaObject.file.size); 

的的mediaObject值:

File: 

buffer: null 
encoding: 7bit 
extension: mp4 
fieldname: file 
mimetype: video/mp4 
name: test1434922812720.mp4 
originalname: test.mp4 
path: uploads/test1434922812720.mp4 
size: 22179964 
truncated: false 

所以我的問題是我是什麼?做錯了?

回答

1

我相信你沒有正確使用res.header方法,因爲你試圖在同一個調用中設置兩個鍵/值對。

方法的正確原型是res.header(field [, value])

我寧願建議使用res.download這可能更多地表明你正在努力實現的目標,併爲你設置適當的標題。

+0

如果即時通訊試圖流 –

+0

這是沒有提到你的問題。爲了流式傳輸,我會在節點中創建一個讀取流(使用fs.createReadStream),並將流傳遞給響應。 –