获取在nodejs中上传的图片url
Getting the image url uploaded in nodejs
我正在使用 nodejs multer 将图像文件上传到图像上传目录。上传后,如何在客户端浏览器的 html img 标签中显示此特定图像。 url是什么?
当我输入 http://localhost:3000/uploads/1591342432.jpeg 时,它说无法获取文件。
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads/");
},
filename: function (req, file, cb) {
cb(null, Date.now() + ".jpeg");
},
});
const upload = multer({ storage: storage });
router.post("/upload", upload.single("file"), async (req, res) => {
try {
console.log("uploading...");
console.log(req.file.filename);
res.json({ cool: "yes" });
// res.json({ file: req.file });
} catch (error) {
console.log(error);
}
});
您似乎正在使用 multer 将 img 上传到名为 uploads 的本地文件夹。
我会推荐以下教程:upload img to firebase
本教程使用multer上传云端firebase。上传图片后,您将获得 link 到存储在云端的图片,同样的 link 您可以在其他服务器上使用
如果您还没有设置,您将需要在尝试通过 http://localhost:3000/1599134489853.jpeg
获取图像资源时设置 GET 路径。对于 Express 应用程序,返回文件的当前 simple/preferred 方法似乎是 res.sendFile. Otherwise, you can generate stream and return as demonstrated here.
我想我需要设置一个静态目录:
app.use(express.static("uploads"));
然后我可以访问图像文件:
http://localhost:3000/1599134489853.jpeg
我正在使用 nodejs multer 将图像文件上传到图像上传目录。上传后,如何在客户端浏览器的 html img 标签中显示此特定图像。 url是什么?
当我输入 http://localhost:3000/uploads/1591342432.jpeg 时,它说无法获取文件。
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads/");
},
filename: function (req, file, cb) {
cb(null, Date.now() + ".jpeg");
},
});
const upload = multer({ storage: storage });
router.post("/upload", upload.single("file"), async (req, res) => {
try {
console.log("uploading...");
console.log(req.file.filename);
res.json({ cool: "yes" });
// res.json({ file: req.file });
} catch (error) {
console.log(error);
}
});
您似乎正在使用 multer 将 img 上传到名为 uploads 的本地文件夹。
我会推荐以下教程:upload img to firebase
本教程使用multer上传云端firebase。上传图片后,您将获得 link 到存储在云端的图片,同样的 link 您可以在其他服务器上使用
如果您还没有设置,您将需要在尝试通过 http://localhost:3000/1599134489853.jpeg
获取图像资源时设置 GET 路径。对于 Express 应用程序,返回文件的当前 simple/preferred 方法似乎是 res.sendFile. Otherwise, you can generate stream and return as demonstrated here.
我想我需要设置一个静态目录:
app.use(express.static("uploads"));
然后我可以访问图像文件:
http://localhost:3000/1599134489853.jpeg