【问题标题】:Sending file to NodeJS from Angular从 Angular 向 NodeJS 发送文件
【发布时间】:2020-01-21 11:49:23
【问题描述】:

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

从一个 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 中,我有以下 sn-p:

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:意外字段

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

谢谢!

【问题讨论】:

  • 能否请您记录错误堆栈,特别是error.field 属性
  • ,下面的答案对你有用吗?

标签: node.js angular multer uppy


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 2018-02-20
    • 2018-05-04
    • 2016-12-27
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多