2017-03-01 29 views
0

我使用的「表達」文件管直接從文件系統

const path = config.storageRoot + '/' + req.params.originalFileName; 
var mimetype = mime.lookup(req.params.originalFileName); 
res.writeHead(200, { 'Content-Type': mimetype}); 
fs.createReadStream(path).pipe(res); 

我獲取XML數據的結果中:

<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.5-c014 79.151739, 2013/04/03-12:12:15  "> 

,當我使用res.end從一個fs.readFile相反,二進制內容的格式正確的結果。

我在做什麼錯了?

回答

0

看看我是如何在這個答案的例子管道文件:

這件事情是這樣的:

// 'type' is the MIME type 
var s = fs.createReadStream(file); 
s.on('open', function() { 
    res.set('Content-Type', type); 
    s.pipe(res); 
}); 
s.on('error', function() { 
    res.set('Content-Type', 'text/plain'); 
    res.status(404).end('Not found'); 
}); 

所以我只是將標題設置爲由Express發佈,而不是顯式發佈標題。另外我正在處理流事件。也許你應該試着同樣這樣做,因爲我做到了,似乎工作的方式,根據特拉維斯:

除了處理流事件和錯誤的另一件事是,以確保你有正確的編碼,權限等等。我不知道你期待什麼結果,XML意味着什麼或來自哪裏,但處理流事件可能會告訴你更多關於正在發生的事情。

+0

如果你使用你的解決方案,我仍然會得到相同的結果。我懷疑xml數據是exif數據。 – programmingheadaches