gpt4 book ai didi

node.js - Node Js 检索并显示图像

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:32 24 4
gpt4 key购买 nike

我能够使用 Node.Js 和 Multer 上传图像。图像以加密格式保存,名称随机。如何检索和显示 .jade 文件或 .hetml 文件中的图像?

//My Code to upload image

router.post("/upload", function(req, res, next){
if (req.files) {
console.log(util.inspect(req.files));
if (req.files.myFile.size === 0) {
return next(new Error("Hey, first would you select a file?"));
}
fs.exists(req.files.myFile.path, function(exists) {
if(exists) {
res.end("Got your file!");
} else {
res.end("Well, there is no magic for those who don’t believe in it!");
}
});
}
});


// Upload page (Jade file)

form#fileUpload(method="post" action="/uploads/upload" enctype="multipart/form-data")
label(for="payload") Select a file to upload:
input#fileToUpload(type='file', name='myFile')
//input#payload(type="file" name="myFile" accept="image/*")
br
button#upload Upload

最佳答案

var multer = require('multer'),
upload = multer({dest: 'uploads/'});
app.post('/multipart-upload', upload.single('file'), function(req, res){
var filename = req.file.path;
console.log('Uploading File: ' + JSON.stringify(req.file));
});

Multer 需要一个上传文件的路径。您可以将路径保留在express的public/static文件夹中,并直接链接该文件。

或者您可以使用静态文件中间件来提供上传文件夹中的文件并直接链接它们。

app.use('/image-uploads',express.static('uploads'));

关于node.js - Node Js 检索并显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33384506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com