2016-03-07 101 views
11

我已经看过很多关于这个问题的答案,但是我还没有找到工作解决方案。我正在尝试制作一个Web应用程序,您可以使用快速和跨平台上传文件,并且遇到没有文件正在上传的问题,并且req.file始终未定义。multer - req.file始终未定义

我的代码如下

'use strict'; 

var express = require('express'); 
var path = require('path'); 
var multer = require('multer') 
var upload = multer({ dest: 'uploads/' }) 

var app = express(); 
require('dotenv').load(); 

app.use(express.static(path.join(__dirname, 'main'))); 

app.post('/upload', upload.single('upl'), function (req, res, next) { 
    // req.file is the `avatar` file 
    // req.body will hold the text fields, if there were any 
    console.log(req.file); 
    res.status(204).end(); 
}) 

var port = process.env.PORT || 8080; 
app.listen(port, function() { 
    console.log('Node.js listening on port ' + port + '...'); 
}); 

形式

<form class="uploadForm" action="/upload" method="post" enctype="multipart/formdata"> 
     <label class="control-label">Select File</label> 
     <input name="upl" id="input-1" type="file" class="file"> 
     <input type="submit" value="submit" /> 
    </form> 

帮助非常感谢,这是推动我疯了。

回答

10

您的enctype有些不正确,应该是multipart/form-data而不是multipart/formdata

+0

非常感谢! – mlamp

20

在邮递员的情况下,尝试以下操作:

  1. 关闭邮递员标签的API
  2. 打开一个新的标签又
  3. 重建的API请求,然后发送。

这可能会解决问题。每当您重新启动服务器时,都需要执行上述再次调用API的步骤。原因在于multer将一些名为connect.sid的Cookie发送给它可能需要进一步通信的客户端。使用旧的cookie不会上传文件。

+0

如果你的邮递员用POST方法打开,并且一些x-www-form-urlencoded变化的表单数据不会发送multipart/form-data,直到你关闭标签并打开一个新邮件为止+1 – kolexinfos

+2

这个答案非常有价值。谢谢。 Open issue FYI https://github.com/postmanlabs/postman-app-support/issues/2602 –

+0

非常感谢!我在想,为什么这不起作用,我正在扯掉我的头发! – user2924127

0

我把我的(有很多我想,肯定更好)的解决方案,帮助很多人喜欢我,因为我在1整天;-(


//JS file on node side 

var express = require('express'); 
var fileUpload = require('express-fileupload'); 
var fs = require("fs"); 
var app = express(); 
console.log('étape 0'); 
app.use(express.static('mesStatic')); 
app.use(fileUpload()); 
console.log('étape 1'); 
app.get('/indexFileUpload.htm', function (req, res) { 
    res.sendFile(__dirname + "/" + "indexFileUpload.htm"); 
}) 
console.log('étape 2'); 
app.post('/file_upload', function (req, res) { 

    console.log('étape 3'); 
    console.log('req.files:' , req.files); 
    if (!req.files) { 
     res.send('No files to upload.'); 
     return; 
    } 

    console.log('req.files.file.data:' , req.files.file.data); 
    var bufDataFile = new Buffer(req.files.file.data, "utf-8"); 
    console.log('étape 3.1'); 
    console.log('__dirname : ' + __dirname); 
    fs.writeFile(__dirname + '/file_upload/output.txt', bufDataFile, function(err) { 
     if (err) { 
     return console.error(err); 
     } 
     else { 
     console.log("Data written successfully !"); 
     }  
     console.log('étape 4'); 
     res.end('Fin OK !!!'); 
    }) 
}) 
var server = app.listen(8081, function() { 
    var host = server.address().address 
    var port = server.address().port 

    console.log("Example app listening at http://%s:%s", host, port); 
}) 
0

HTML文件,搜查

<form class="uploadForm" action="/upload" method="post" enctype="multipart/form-data"> 
    <label class="control-label">Select File</label> 
    <input name="upl" id="input-1" type="file" class="file"> 
    <input type="submit" value="submit" /> 
</form> 

app.js

var express=require("express"); 
var multer=require("multer"); 
var app=express(); 
var upload=multer({dest:"uploads/"}); 
app.post("/upload",upload.single("upl"),function(req,res){ 
console.log("Uploaded Successfull with filename : "+req.upl.filename); 
}); 
0

是的,您的enctype是错误的,这是唯一的问题。确保你改正了你的enctype,否则你可能会在req.file或req.files中得到undefined。