1
<form action="http://localhost:3000/examples" method="post" enctype="multipart/form-data" accept="application/json">
    <input type="text" name ="name">
    <input type="text" name ="codedescription">
    <input type="file" name ="file">
    <input type="submit" value="Upload selected file to server">
</form>

var multer  = require('multer');
    app.use(multer({ dest: './uploads/',
        onFileUploadStart : function(file){
            console.log('File recieved:');
            console.log(file);
        },
        onFileUploadData:function (file,data){
            console.log('Data recieved');
        },
        onParseEnd: function(req,next){
            next();
        }
    }));
app.route('/examples').post(users.requiresLogin, examples.create);

exports.create = function(req, res) {
        console.log("req.files"+req.files);
        console.log("req.name"+req.body.name);
        console.log("req.codedescription"+req.body.codedescription);
    };

提交没有 enctype="multipart/form-data" 的表单正在工作,但我无法获取文件。

使用 enctype="multipart/form-data" 提交表单正在工作,但我无法获取文件和数据。

4

2 回答 2

1

你可以试试这个

<html>
<head>
<title>FileUpload</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<form id       =  "uploadForm"
     enctype   =  "multipart/form-data"
     action    =  "http://localhost:3000/api/photo"
     method    =  "post"
>
<input type="file" name="userPhoto" multiple />
<input type="submit" value="Upload Image" name="submit" id="btnUpload">
<span id="spnStatus" />
</form>

<script>
        $(document).ready(function(){
            $('#btnUpload').click(function(){
                $('#spnStatus').empty().text("File is Uploading");
                $(this).ajaxSubmit({
                 error : function(xhr){
                    status('Error : '+xhr.status);
                    }

                 success : function(response){
                    $('#spnStatus').empty().text(xhr);
                    }
                });
            });

        });
    </script>
</body>
</html>

NodeJS 快递

var express =   require("../node_modules/express");
var multer  =   require('../node_modules/multer');
var bodyParser =    require("../node_modules/body-parser");
var app =   express();
app.use(bodyParser.json());


var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now());
  }
});
var upload = multer({ storage : storage }).array('userPhoto',8);

app.get('/',function(req,res){
      res.sendFile(__dirname + "/fileUpload.html");
});

app.post('/api/photo',function(req,res){

          upload(req,res,function(err) {
            if(err) {
              console.log(err);
                return res.end("Error uploading file.");
            }
            res.end("File is uploaded");
         });
});

app.listen(3001,function(){
    console.log("Working on port 3001");
});

现在您一次最多可以上传 8 个文件,如果要上传超过 8 个,只需编辑 var upload = multer({ storage : storage }).array('userPhoto','LIMITHERE');

于 2016-02-29T10:19:10.100 回答
0

@karthik comment, i think you need this as well including after jquery, when you want to use his example:

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js"></script>
于 2018-06-14T08:52:27.670 回答