繁体   English   中英

将文件从 Angular 发送到 NodeJS

[英]Sending file to NodeJS from Angular

这是一个常见的问题,但是我仍然没有设法完成这项工作。

从 Angular 应用程序中,使用 uppy,我的组件中有以下代码:

const XHRUpload = require('@uppy/xhr-upload')
    this.uppy = new Uppy({
      id: 'uppyContent',
      debug        : true,
      autoProceed  : false,
      restrictions : {
        //allowedFileTypes : [ 'image/*', 'video/*','audio/*','application/pdf','.obj','.FBX','.OBJ','.fbx' ],
        allowedFileTypes: [fileTypes],
        maxNumberOfFiles: 1
      },
      meta : {
         ProjectID: this.projectID, Componente: componente
      }
    })
      .use(Dashboard, {
        trigger              : '.UppyModalOpenerBtn',
        inline               : true,
        target               : '.DashboardContainer',
        replaceTargetContent : true,
        note                 : fileTypeDescription+' only, 1 file, up to 1 GB',
        maxHeight            : 450,
        metaFields           : [
          { id : 'license', name : 'License', placeholder : 'specify license' },
          {
            id          : 'caption',
            name        : 'Caption',
            placeholder : 'describe what the image is about'
          }
        ]
      })
      .use(XHRUpload, {
        endpoint: this.globalService.API_PATH + 'urluploadhere',
        formData: true,
        fieldName: 'files[]'
      })
      //.use(Tus, { endpoint : this.globalService.API_PATH + 'globalProjectFileUpload', resume : true })
      .use(RestoreFiles, {
        serviceWorker : true,
        indexedDB     : {
          maxFileSize  : 1024 * 1024 * 1024, // 1GB => Each file
          maxTotalSize : 1024 * 1024 * 1024 * 1024 // 1 TB

        }
      })
      .run();

以下是我的 Chrome 调试器的屏幕截图,使用网络选项卡:

在此处输入图片说明

在我的 NodeJS 中,我有以下代码段:

var bodyParser = require("body-parser");
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

var multer = require( 'multer');
var upload = multer();
app.use(upload.array());

app.post('/v1/urluploadhere', function(req, res) {
  console.log(req.body.files);
  res.end();
});

但是,它会因以下原因崩溃

MulterError:意外的字段

我应该怎么做才能纠正这个问题?

谢谢!

尝试删除app.use(upload.array()); 行并使用app.use(upload.single('field name in the formData'))因为您正在尝试上传单个文件。

如果您尝试发送文件数组,请在数组方法app.use(upload.array('field name in the formData'));指定 fieldName (files[] app.use(upload.array('field name in the formData'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM