Send blob data to node using fetch, multer, express

前端 未结 1 1535
青春惊慌失措
青春惊慌失措 2021-02-04 08:35

Trying to send a blob object to my node server. On the client side I\'m recording some audio using MediaRecorder and then I want to send the file to my server for processing.

1条回答
  •  日久生厌
    2021-02-04 09:03

    I was just able to run a minimum configuration of your above example and it worked fine for me.

    Server:

    var express = require('express');
    var multer  = require('multer');
    var app = express();
    
    app.use(express.static('public')); // for serving the HTML file
    
    var upload = multer({ dest: __dirname + '/public/uploads/' });
    var type = upload.single('upl');
    
    app.post('/api/test', type, function (req, res) {
       console.log(req.body);
       console.log(req.file);
       // do stuff with file
    });
    
    app.listen(3000);
    

    HTML file in public:

    
    

    The console.log(myBlob); on the frontend is printing Blob {size: 23, type: "text/plain"}. The backend is printing:

    {}
    { fieldname: 'upl',
      originalname: 'blobby.txt',
      encoding: '7bit',
      mimetype: 'text/plain',
      destination: '/var/www/test/public/uploads/',
      filename: 'dc56f94d7ae90853021ab7d2931ad636',
      path: '/var/www/test/public/uploads/dc56f94d7ae90853021ab7d2931ad636',
      size: 23 }
    

    Maybe also try it with a hard-coded Blob like in this example for debugging purposes.

    0 讨论(0)
提交回复
热议问题