2015-09-08 37 views
2

在我的前端html中,我想要一個帶有源代碼'/ uploads/profilePicture'的img標記。在node/express中處理請求時,我想對用戶進行身份驗證,從文件系統加載用戶的個人資料圖片並使用圖像數據進行響應。我的代碼基本上歸結爲:嘗試使用內容類型圖像/ jpeg響應時發生圖像破損

app.get('*', function(req, res, next) { 
    /* Some code leading to the function getProfilePicture */ 
}); 

function getProfilePicture(req, res, next) { 
    loadFile('uploads/' + req.user.local.picture, function(image) { 
     res.writeHead(200, { 'Content-Type': 'image/jpeg' }); 
     res.end(image); 
    }); 
}; 

當代碼運行,前端顯示破碎的形象,但沒有任何錯誤都印在控制檯中。

有趣的是,這以前爲我工作。前一段時間,我的路由代碼很不通用,因爲它在我的網站上爲每個頁面使用了app.get()。所以我用app.get('/uploads/profilePicture')而不是app.get('*')。這可能是問題嗎?

編輯:

我使用節點v0.10.35和表達3.0.6。

編輯2:

與快遞3.19.1測試,仍然沒有工作。

回答

0

原來,使用res.sendfile的伎倆。

res.status(200).sendfile(URL); 

任何解釋爲什麼sendfile工作,但不是setHeaders +結束將不勝感激!

相關問題