微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Multer返回req.file undefined

hey, I kNow this question has been asked plenty of times before, and I’ve tried implementing those solutions, but they don’t really work for me.

I have been tearing my hair out trying to figure out how to upload a file and read the file size through Node. I initially tried using the formidable npm, which seems to no longer be maintained as I can’t find documentation on it. I had no way of dealing with the errors so I tried using multer. However, I repeatedly get an undefined log when I try to log req.file.

I have the server.js code below

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

var app = express();

var PORT = 8080;

app.use(express.static(__dirname+'/views'));

app.set('views', './views');

app.set('view engine', 'jade');

app.get('/', function(req, res){
   res.render('index.jade'); 
});

app.post('/upload', upload.single('Upload'),function(req, res){
    console.log(req.file);
});

app.listen(PORT, function(){
    console.log('Express listening on port: '+PORT);
});

My javascript code with the AJAX call is provided below

$('#upload-butt').on('change', function(){
      var file = $(this).get(0).files;
      console.log(typeof file);

      if(file.length > 0){
          var formData = new FormData();
          formData.append('Upload', file, file.name);
          $.ajax({
              url: '/upload', 
              type: 'POST',
              data:formData,
              processData:false,
              contentType:false,
              error: function(jXhr, status){
                  console.log('error: '+status);  
              },
              success: function(data){
                  console.log('upload successful: '+data);
              }
          })
      }
   });

My index.jade code is given below

html
head
    link(rel='stylesheet', href='style.css', type='text/css')
    title Upload file for shortening
body
    h1 Welcome to file Metadata service
    div(id='upload-button')
        form(enctype='multipart/form-data', method='post', action='/upload')
            input(name='Upload', type='file', id='upload-butt')
    div(id="submit-button")
        form(action = '/submit')
            button(type="submit", value='Submit', id='submit-butt') Submit

    script(src="https://code.jquery.com/jquery-2.2.0.min.js")
    script(src="upload.js")

I am ready to tear my hair out, so I will be very grateful to anyone who can help me here! Thanks!

解决方法:

您没有正确应用中间.

你可以使用这样的东西:

var express = require('express')
var multer  = require('multer')

var app = express()
app.use(multer({ dest: './uploads/'}))
You can access the fields and files in the request object:

console.log(req.body)
console.log(req.files)

所以在你的代码中你必须同样适用

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

var app = express();

var PORT = 8080;

app.use(upload);

app.use(express.static(__dirname+'/views'));

app.set('views', './views');

app.set('view engine', 'jade');

app.get('/', function(req, res){
   res.render('index.jade'); 
});

app.post('/upload', upload.single('Upload'),function(req, res){
    console.log(req.file);
});

app.listen(PORT, function(){
    console.log('Express listening on port: '+PORT);
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐