Upload video on node js that I do wrong

0

I have a special server to upload files in nodejs that is this

var express = require("express");
var app = express();
var multer, storage, path, crypto;
multer = require('multer')
path = require('path');
crypto = require('crypto');

var form = "<!DOCTYPE HTML><html><body>" +
"<form method='post' action='/upload' enctype='multipart/form-data'>" +
"<input type='file' name='upload'/>" +
"<input type='submit' /></form>" +
"</body></html>";

app.get('/', function (req, res){
  res.writeHead(200, {'Content-Type': 'text/html' });
  res.end(form);

});

// Include the node file module
var fs = require('fs');

storage = multer.diskStorage({
    destination: './uploads/',filename: function(req, file, callback) {
        callback(null,  Date.now() + "_" + file.originalname);
    }
    /* filename: function(req, file, cb) {
      return crypto.pseudoRandomBytes(16, function(err, raw) {
        if (err) {
          return cb(err);
        }
        return cb(null, "" + (raw.toString('hex')) + (path.extname(file.originalname)));
      });
    } */
  });


// Post files
app.post(
  "/upload",
  multer({
    storage: storage
  }).single('upload'), function(req, res) {
    console.log(req.file);
    console.log(req.body);
    /* res.redirect("/uploads/" + req.file.filename); */
    console.log(req.file.filename);
    return res.status(200).end();
  });

app.get('/uploads/:upload', function (req, res){
  file = req.params.upload;
  console.log(req.params.upload);
  var img = fs.readFileSync(__dirname + "/uploads/" + file);
  res.writeHead(200, {'Content-Type': 'image/png' });
  res.end(img, 'binary');

});

port = 4000;
app.listen(port);
console.log("listen on port " + port)

and I'm trying to upload a video file from android with the code I got on this page

link

but it generates the following error when trying to send the video

  

MulterError: Unexpected field       at wrappedFileFilter (C: \ Users \ ray_c \ OneDrive \ Documents \ jdserver \ node_modules \ multer \ index.js: 40: 19)       at Busboy. (C: \ Users \ ray_c \ OneDrive \ Documents \ jdserver \ node_modules \ multer \ lib \ make-middleware.js: 114: 7)       at emitMany (events.js: 147: 13)       at Busboy.emit (events.js: 224: 7)       at Busboy.emit (C: \ Users \ ray_c \ OneDrive \ Documents \ jdserver \ node_modules \ busboy \ lib \ main.js: 38: 33)       at PartStream. (C: \ Users \ ray_c \ OneDrive \ Documents \ jdserver \ node_modules \ busboy \ lib \ types \ multipart.js: 213: 13)       at emitOne (events.js: 116: 13)       at PartStream.emit (events.js: 211: 7)       at HeaderParser. (C: \ Users \ ray_c \ OneDrive \ Documents \ jdserver \ node_modules \ dicer \ lib \ Dicer.js: 51: 16)       at emitOne (events.js: 116: 13)

Do you have any idea what is happening and how can I fix it?

    
asked by Jesus Cabrita 15.11.2018 в 14:36
source

1 answer

0

Shortly after uploading the question I discovered the solution in this line of code

app.post(
  "/upload",
  multer({
    storage: storage
>   }).single('upload'), function(req, res) {
    console.log(req.file);
    console.log(req.body);
    /* res.redirect("/uploads/" + req.file.filename); */
    console.log(req.file.filename);
    return res.status(200).end();
  });

should be replaced upload by myFile or on the contrary, change the name of the file to upload from the android, anyway I managed to upload a video

    
answered by 15.11.2018 в 15:04