<small id='UYLsh'></small><noframes id='UYLsh'>

      • <bdo id='UYLsh'></bdo><ul id='UYLsh'></ul>
      <i id='UYLsh'><tr id='UYLsh'><dt id='UYLsh'><q id='UYLsh'><span id='UYLsh'><b id='UYLsh'><form id='UYLsh'><ins id='UYLsh'></ins><ul id='UYLsh'></ul><sub id='UYLsh'></sub></form><legend id='UYLsh'></legend><bdo id='UYLsh'><pre id='UYLsh'><center id='UYLsh'></center></pre></bdo></b><th id='UYLsh'></th></span></q></dt></tr></i><div id='UYLsh'><tfoot id='UYLsh'></tfoot><dl id='UYLsh'><fieldset id='UYLsh'></fieldset></dl></div>

        <legend id='UYLsh'><style id='UYLsh'><dir id='UYLsh'><q id='UYLsh'></q></dir></style></legend>

      1. <tfoot id='UYLsh'></tfoot>

      2. 使用 fetch、multer、express 将 blob 数据发送到节点

        时间:2023-10-02
        • <tfoot id='5V9wk'></tfoot>

          <small id='5V9wk'></small><noframes id='5V9wk'>

              <tbody id='5V9wk'></tbody>
            • <bdo id='5V9wk'></bdo><ul id='5V9wk'></ul>

                <i id='5V9wk'><tr id='5V9wk'><dt id='5V9wk'><q id='5V9wk'><span id='5V9wk'><b id='5V9wk'><form id='5V9wk'><ins id='5V9wk'></ins><ul id='5V9wk'></ul><sub id='5V9wk'></sub></form><legend id='5V9wk'></legend><bdo id='5V9wk'><pre id='5V9wk'><center id='5V9wk'></center></pre></bdo></b><th id='5V9wk'></th></span></q></dt></tr></i><div id='5V9wk'><tfoot id='5V9wk'></tfoot><dl id='5V9wk'><fieldset id='5V9wk'></fieldset></dl></div>
                1. <legend id='5V9wk'><style id='5V9wk'><dir id='5V9wk'><q id='5V9wk'></q></dir></style></legend>
                2. 本文介绍了使用 fetch、multer、express 将 blob 数据发送到节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  尝试将 blob 对象发送到我的节点服务器.在客户端,我正在使用 MediaRecorder 录制一些音频,然后我想将文件发送到我的服务器进行处理.

                  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.

                        saveButton.onclick = function(e, audio) {
                          var blobData = localStorage.getItem('recording');
                          console.log(blobData);
                  
                          var fd = new FormData();
                          fd.append('upl', blobData, 'blobby.raw');
                  
                          fetch('/api/test',
                            {
                              method: 'post',
                              body: fd
                            })
                          .then(function(response) {
                            console.log('done');
                            return response;
                          })
                          .catch(function(err){ 
                            console.log(err);
                          });
                  
                        }
                  

                  这是我的快速路线,使用 multer:

                  This is my express route, which uses multer:

                    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
                    });
                  

                  但我的日志什么也没返回:

                  But my logs return nothing:

                  { upl: '' }
                  undefined
                  

                  在这方面花了很长时间,所以任何帮助表示赞赏!

                  Been spending a long time on this so any help appreciated!

                  推荐答案

                  我刚刚能够运行上述示例的最低配置,它对我来说很好.

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

                  服务器:

                  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);
                  

                  public中的HTML文件:

                  <script>
                  var myBlob = new Blob(["This is my blob content"], {type : "text/plain"});
                  console.log(myBlob);
                  
                  // here unnecessary - just for testing if it can be read from local storage
                  localStorage.myfile = myBlob;
                  
                  var fd = new FormData();
                  fd.append('upl', localStorage.myfile, 'blobby.txt');
                  
                  fetch('/api/test',
                  {
                      method: 'post',
                      body: fd
                  }); 
                  </script>
                  

                  前端的 console.log(myBlob); 正在打印 Blob {size: 23, type: "text/plain"}.后端正在打印:

                  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 }
                  

                  也许也可以尝试使用硬编码的 Blob,如本示例中用于调试目的.

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

                  这篇关于使用 fetch、multer、express 将 blob 数据发送到节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:发送自定义用户代理字符串以及我的标头(获取) 下一篇:如何使用 Fetch API 下载和保存文件?(Node.js)

                  相关文章

                    <i id='K1WiF'><tr id='K1WiF'><dt id='K1WiF'><q id='K1WiF'><span id='K1WiF'><b id='K1WiF'><form id='K1WiF'><ins id='K1WiF'></ins><ul id='K1WiF'></ul><sub id='K1WiF'></sub></form><legend id='K1WiF'></legend><bdo id='K1WiF'><pre id='K1WiF'><center id='K1WiF'></center></pre></bdo></b><th id='K1WiF'></th></span></q></dt></tr></i><div id='K1WiF'><tfoot id='K1WiF'></tfoot><dl id='K1WiF'><fieldset id='K1WiF'></fieldset></dl></div>
                      <bdo id='K1WiF'></bdo><ul id='K1WiF'></ul>

                    1. <legend id='K1WiF'><style id='K1WiF'><dir id='K1WiF'><q id='K1WiF'></q></dir></style></legend>

                      <small id='K1WiF'></small><noframes id='K1WiF'>

                      <tfoot id='K1WiF'></tfoot>