上传图片node.js表达

时间:2015-10-27 10:40:00

标签: node.js express

我正在尝试将图像上传到node.js中的服务器并表达。

我的玉图片上传表单:

include layout
div.container
    h2 Image Upload Form
    form#fileUpload(method="post" action="/upload" enctype="multipart/form-data") 
        label(for="payload") Select a file to upload:     
        input#payload(type="file" name="myFile" accept="image/*") 
        br 
        button#upload Upload

在我处理回复和展示形式的index.js中:

router.get('/imageUpload', function(req, res, next){
    res.render('imageUpload', { title: 'Image upload form' });
});

router.post("/upload", function(req, res, next){ 
    console.log(req.files);
});

当我尝试undefined时,我收到req.files错误;我是节点的新手,所以请帮忙。 谢谢。

2 个答案:

答案 0 :(得分:3)

最后图片上传是感谢Lucas Costa的帮助,知道我分享了我的代码

步骤1.从官方网站formidable

安装强大的

在提示符上运行命令

npm install formidable@latest

步骤2.我的图片上传表格在 .jade

include layout
    div.container
        h2 Image Upload Form
        form#fileUpload(method="post" action="/upload" enctype="multipart/form-data") 
            label(for="payload") Select a file to upload:     
            input#payload(type="file" name="myFile" accept="image/*") 
            br 
            button#upload Upload

步骤3.在我的案例中添加模块我有 index.js

var express = require('express');
var router = express.Router();
var util = require("util"); 
var fs = require("fs");
var formidable = require('formidable');
var path = require('path');

/* GET home page. */

router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express', dataGet: "data" });
});

router.get('/about', function(req, res, next) {
    res.render('index', { title: 'C' });
});

router.get('/imageUpload', function(req, res, next){
    res.render('imageUpload', { title: 'Image upload form' });
});

router.post("/upload", function(req, res, next){ 
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        // `file` is the name of the <input> field of type `file`
        console.log(files);
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write('received upload:\n\n');
        res.end(util.inspect({fields: fields, files: files}));
    });
    form.on('error', function(err) {
        console.error(err);
    });
    form.on('progress', function(bytesReceived, bytesExpected) {
        var percent_complete = (bytesReceived / bytesExpected) * 100;
        console.log(percent_complete.toFixed(2));
    });
    form.on('end', function(fields, files) {
        /* Temporary location of our uploaded file */
        var temp_path = this.openedFiles[0].path;
        /* The file name of the uploaded file */
        var file_name = this.openedFiles[0].name;
        /* Location where we want to copy the uploaded file */
        var new_location = 'F:/node/expressApp/myAppExpress/public/images/';

        fs.readFile(temp_path, function(err, data) {
            fs.writeFile(new_location + file_name, data, function(err) {
                fs.unlink(temp_path, function(err) {
                    if (err) {
                        console.error(err);
                        } else {
                        console.log("success!");
                    }
                });
            });
        });
    });
});

router.get('/:username', function(req, res) {
    // make somethings with username
    var username = req.params.username;
    console.log("get username"+ username);
});

module.exports = router;

全部完成!希望这会对某人有所帮助: - )

答案 1 :(得分:1)

undefined正在获取,因为不存在req.files

改变:

console.log(req.body) // or req.body.myFile to get information of the input

对于上传图片,建议您使用formidable,有些像this