How to upload a video using Multipart and Node.js?

Asked

Viewed 922 times

1

I have an application that is being written using Node.Js, in which I use Ffmpeg to create a video from a webcam. I need to upload it to the server using a POST request. So the question is:

How to upload a video using Multipart and Node.js?

Ps1.: Video is a local file whose path will be passed by parameter! Creating a form to choose the file is not the answer.

Ps2.: I’m not using Express.

Ps3.: The server runs on the Internet, for this request it receives the data in an address like: application.com.br/v1/videos. I don’t have access to which folder will be saved or the communication port. And it still needs to send parameters in the header and the request body.


Edit 1: I was able to request using the module request. The problem is that I need to add an integer parameter to the request body. "testeId": 7451109. How to proceed? Following code so far.

Edit 2: Updating the code.

Code:

request({
        method: "POST",
        url: "https://" + options.host + options.path,
        formData: {
            upload_type: "mp4",
            file: fs.createReadStream(options.nomeVideo)
        },
        headers: {
            'Content-Type': 'multipart/form-data',
            'access-token': chaveCriptografada
        }
    },

    function (error, response, body) {
        console.log(error);
        console.log(response);
        console.log(body);
    });

1 answer

1

Hello, take a look at that lib.

https://github.com/expressjs/multer

Full example:

https://github.com/wandersonpereira/multer-sem-express

This is an example without using express. To use it just have a request that moves a file.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

var multer  = require('multer')

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '/tmp/my-uploads')
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
})
  
const server = http.createServer((req, res) => {
    var upload = multer({ storage: storage }).single('file');  

    upload(req, res, function (err) {
        if (err) {
            console.log(err);
            return
        }
        // Everything went fine
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain');
        res.end('Hello World\n');
    });    
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

  • As I said in the question, I am not using Express. But thanks for the answer!

  • No need to use express, I put up an example of code used the default http of Node.

  • In the case of my server, it is already running on the internet, a site like app.com.br/v1/videos. I don’t have the communication port. Is there still a way to use the multer? If so, how to proceed?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.