簡體   English   中英

使用node.js和express.js的圖像顯示錯誤

[英]image display error using node.js and express.js

一旦我打開“ localhost:3000”,圖像就會下載到我的筆記本電腦上,而不是顯示在網頁上,有人可以幫我嗎?

var express= require('express');
var fs = require('fs');
var app= express();

var imgpath= 'C:\Users\Rohit\Downloads\images.jpg';

app.get('/',function(req,res){
  res.send(fs.readFileSync(imgpath));
});

app.listen(3000);
console.log('listening');

您必須設置標題。

app.get('/',function(req,res){
  res.set('Content-Type', 'image/jpg');//Added line
  res.send(fs.readFileSync(imgpath));
});

您必須設置內容類型

res.type('jpg').send(fs.readFileSync(imgpath));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM