【问题标题】:Kendo UI Angular Upload File to Backend as Node.JSKendo UI Angular 将文件作为 Node.JS 上传到后端
【发布时间】:2019-11-29 20:25:20
【问题描述】:

我正在使用 Kendo Upload Control 将文件上传到使用 GridFS multer 的 Node.js 后端。

角度

<kendo-upload 
  [saveField]="file"
  [withCredentials]="false"
  [saveUrl]="uploadUrl"
  (autoUpload)="false"
  [multiple]="false"
  (select)="selectProfilePic($event)"></kendo-upload>

但是 node.js API 没有接收请求。我正在使用[saveField]="file" 传递上传的文件和下面的node.js。

var storage = new GridFsStorage({
    //url: mongoose.connection.client.s.url,
    //options: options,
    db: mongoose.connection,
    file: (req, file) => {
      return new Promise((resolve, reject) => {
        myCrypto.randomBytes(16, (err, buf) => {
          if (err) {
            return reject(err);
          }
          const filename = buf.toString('hex') + path.extname(file.originalname);
          const fileInfo = {
            filename: filename,
            bucketName: 'uploads'
          };
          resolve(fileInfo);
        });
      });
    }
  });



const upload = multer({ storage });
router.post('/upload', upload.single('file'), fileUpload);

module.exports = router;

function fileUpload(req, res) {

 console.log("fileUpload")
  try {
    res.send({ file: req.file })
  }
  catch(err){

    console.log(err);
    res.send(err)
  }
}

日志

2019-07-21T19:34:33.679205+00:00 app[web.1]:文件控制器 2019-07-21T19:34:33.680436+00:00 应用 [web.1]: {} 2019-07-21T19:34:33.983631+00:00 app[web.1]: MulterError: Unexpected 字段 2019-07-21T19:34:33.983647+00:00 应用 [web.1]:在 WrappedFileFilter (/app/node_modules/multer/index.js:40:19) 2019-07-21T19:34:33.983649+00:00 应用 [web.1]:在 Busboy。 (/app/node_modules/multer/lib/make-middleware.js:114:7) 2019-07-21T19:34:33.983650+00:00 应用 [web.1]:在 Busboy.emit (events.js:198:13) 2019-07-21T19:34:33.983670+00:00 应用 [web.1]:在 Busboy.emit (/app/node_modules/busboy/lib/main.js:38:33) 2019-07-21T19:34:33.983671+00:00 应用 [web.1]:在 PartStream。 (/app/node_modules/busboy/lib/types/multipart.js:213:13) 2019-07-21T19:34:33.983673+00:00 应用 [web.1]:在 PartStream.emit (events.js:198:13) 2019-07-21T19:34:33.983674+00:00 应用 [web.1]:在 标头解析器。 (/app/node_modules/dicer/lib/Dicer.js:51:16) 2019-07-21T19:34:33.983675+00:00 应用 [web.1]:在 HeaderParser.emit (events.js:198:13) 2019-07-21T19:34:33.983677+00:00 应用 [web.1]:在 HeaderParser._finish (/app/node_modules/dicer/lib/HeaderParser.js:68:8) 2019-07-21T19:34:33.983678+00:00 app[web.1]:在 SBMH。 (/app/node_modules/dicer/lib/HeaderParser.js:40:12) 2019-07-21T19:34:33.983679+00:00 应用 [web.1]:在 SBMH.emit (events.js:198:13) 2019-07-21T19:34:33.983680+00:00 应用 [web.1]:在 SBMH._sbmh_feed (/app/node_modules/streamsearch/lib/sbmh.js:159:14) 2019-07-21T19:34:33.983682+00:00 应用 [web.1]:在 SBMH.push (/app/node_modules/streamsearch/lib/sbmh.js:56:14) 2019-07-21T19:34:33.983683+00:00 应用 [web.1]:在 HeaderParser.push (/app/node_modules/dicer/lib/HeaderParser.js:46:19) 2019-07-21T19:34:33.983685+00:00 应用 [web.1]:在 Dicer._oninfo (/app/node_modules/dicer/lib/Dicer.js:197:25) 2019-07-21T19:34:33.983686+00:00 应用 [web.1]:在 SBMH。 (/app/node_modules/dicer/lib/Dicer.js:127:10) 2019-07-21T19:34:33.989908+00:00 heroku[路由器]: at=info method=POST path="/v1/file/upload" 主机=herokuapp.com request_id=aa1010df-d244-46bc-9b36-f8e437d5ad2a fwd="80.233.46.84" dyno=web.1 连接=0ms 服务=312ms 状态=500 字节=286 协议=https

【问题讨论】:

  • 你能显示一些请求日志吗?比如响应状态/错误信息是什么?
  • 500 内部服务器错误,我没有收到任何详细的错误消息。
  • @GProst,如果您看到更新的代码,我已经稍微更改了fileUpload 方法,我注意到文件上传没有被命中。
  • Node 日志中有错误详细信息吗?也许您可以在您的 Express 应用程序中创建一个全局错误处理程序来捕获并记录错误。此外,检查(使用控制台日志)是否进入生成文件名的函数(在 Promise 中)。
  • expressjs.com/en/guide/error-handling.html 检查“默认错误处理程序”部分。在那里添加一些记录器

标签: node.js angular kendo-ui-angular2 multer-gridfs-storage


【解决方案1】:

您是否有可能将字段名称设置为某个 file 变量?因此,我相信您希望 [saveField]="file" 将字段名称设置为 'file' 字符串,但它会搜索一些 this.filevariable 即 undefined,因此您将字段名称设置为默认的 'files' 值?

【讨论】:

  • 太棒了!这就是原因..[saveField]="'file'" 工作
【解决方案2】:

遵循@GProst 的建议并进行了一些分析,以下修复有效,我还不知道解决方案。

根据Kendo UI angular documentation

设置包含提交到saveUrl 的文件的FormData 键。 默认值为files

所以,我只是将参数名称从 file 更改为 files 并且它起作用了。

const upload = multer({ storage });
router.post('/upload', upload.single('files'), fileUpload);

【讨论】:

  • 这很奇怪,saveField 应该有效。我没有使用过 Kendo UI,但在他们的 GitHub 存储库中没有发现任何关于设置字段名称的问题
  • 是的,但它不起作用,也许我可以向 Kendo 报告这个问题
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 2013-10-24
  • 2020-09-20
相关资源
最近更新 更多