簡體   English   中英

Kendo UI Angular將文件作為Node.JS上傳到后端

[英]Kendo UI Angular Upload File to Backend as Node.JS

我正在使用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"來傳遞上傳的文件和下面的[saveField]="file"

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 app [web.1]:{} 2019-07-21T19: 34:33.983631 + 00:00 app [web.1]:MulterError:意外字段2019-07-21T19:34:33.983647 + 00:00 app [web.1]:在wrappedFileFilter(/ app / node_modules / multer / index。 js:40:19)2019-07-21T19:34:33.983649 + 00:00 app [web.1]:在Busboy上。 (/app/node_modules/multer/lib/make-middleware.js:114:7)2019-07-21T19:34:33.983650 + 00:00 app [web.1]:在Busboy.emit(events.js:198 :13)2019-07-21T19:34:33.983670 + 00:00 app [web.1]:在Busboy.emit(/app/node_modules/busboy/lib/main.js:38:33)2019-07-21T19 :34:33.983671 + 00:00 app [web.1]:在PartStream上。 (/app/node_modules/busboy/lib/types/multipart.js:213:13)2019-07-21T19:34:33.983673 + 00:00 app [web.1]:在PartStream.emit(events.js:198 :13)2019-07-21T19:34:33.983674 + 00:00 app [web.1]:位於HeaderParser。 (/app/node_modules/dicer/lib/Dicer.js:51:16)2019-07-21T19:34:33.983675 ​​+ 00:00 app [web.1]:位於HeaderParser.emit(events.js:198:13 )2019-07-21T19:34:33.983677 + 00:00 app [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 app [web.1]:在SBMH.emit(events.js:198:13 )2019-07-21T19:34:33.983680 + 00:00 app [web.1]:位於SBMH._sbmh_feed(/app/node_modules/streamsearch/lib/sbmh.js:159:14)2019-07-21T19:34 :33.983682 + 00:00 app [web.1]:在SBMH.push(/app/node_modules/streamsearch/lib/sbmh.js:56:14)2019-07-21T19:34:33.983683 + 00:00 app [ web.1]:在HeaderParser.push(/app/node_modules/dicer/lib/HeaderParser.js:46:19)2019-07-21T19:34:33.983685 + 00:00 app [web.1]:在Dicer。 _oninfo(/app/node_modules/dicer/lib/Dicer.js:197:25)2019-07-21T19:34:33.983686 + 00:00 app [web.1]:在SBMH。 (/app/node_modules/dicer/lib/Dicer.js:127:10)2019-07-21T19:34:33.989908 + 00:00 heroku [router]:at = info method = POST path =“ / v1 / file /上傳“ host = herokuapp.com request_id = aa1010df-d244-46bc-9b36-f8e437d5ad2a fwd =” 80.233.46.84“ dyno = web.1 connect = 0ms service = 312ms status = 500 bytes = 286 protocol = https

您是否有可能將字段名稱設置為某個file變量? 因此,我相信您希望[saveField]="file"將字段名稱設置為'file'字符串,但是它會搜索一些undefined this.file變量,因此您將字段名稱設置為默認的'files'值?

遵循@GProst的建議並進行了一些分析,以下修復有效,但我尚不知道解決方案。

根據Kendo UI角度文檔

設置FormData鍵,該鍵包含提交到saveUrl的文件。 默認值為files

因此,我只是將參數名稱從file更改為files並且它起作用了。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM