2016-11-09 51 views
2

我有一個jpeg圖像二進制文件,我如何使用Hapi來顯示圖像?我的代碼只向API的最終用戶顯示垃圾。Hapi:如何發送圖像

hapiServer.route({ 
    method: 'GET', 
    path:'/users/{userId}/photo', 
    handler: async function (request, reply) { 
    const userId = parseInt(encodeURIComponent(request.params.userId)); 


    const photo = getImageBinary(userId);  
    reply(photo); 
    } 
}); 
+0

怎麼叫這個API。 getImageBinary返回什麼? –

+0

@JaromandaX用戶輸入www.host.com/users/123/photo,瀏覽器顯示照片,getImageBinary返回圖像的二進制數據 – omidh

+0

內容類型頭應該有助於 –

回答

1

假設您的圖片二進制數據png

hapiServer.route({ 
    method: 'GET', 
    path:'/users/{userId}/photo', 
    handler: async function (request, reply) { 
    const userId = parseInt(encodeURIComponent(request.params.userId)); 

    const photo = getImageBinary(userId);  
    reply(photo).header('Content-Disposition','inline').header('Content-type','image/png'); 
    } 
});