节点Js检索并显示图像

时间:2015-10-28 06:41:25

标签: node.js image

我可以使用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

1 个答案:

答案 0 :(得分:1)

food_id

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

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

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)); });