Under the Nodejs+webstorm+mysql environment, when using formidable to upload multiple files, it is always found that there is always an empty file. Ask for advice.

problem description

when uploading multiple files using formidable, there is always one more empty file, as shown in the following figure

clipboard.png

so the traversal file stream is printed out, and sure enough, it is looped three times, and the code is as follows:

related codes

var form = new formidable.IncomingForm();
form.uploadDir = "public" + AVATAR_UPLOAD_FOLDER;     //
form.keepExtensions = true;     //
form.maxFieldsSize = 2 * 1024 * 1024;   //
var newPath;
var allFile=[];
form.on("file", function (filed, file) {
    console.log("filed:::",filed);
    console.log("file:::",file);
    allFile.push([filed, file]);//
    // console.log("**********=>allFile:",allFile);
}).parse(req, function (err, fields, files) {

    var extName = "";  //
    if(files.file && files.file.length>0){
        console.log("0");
        switch (files.file.type) {
            case "image/pjpeg":
                extName = "jpg";
                break;
            case "image/jpeg":
                extName = "jpg";
                break;
            case "image/png":
                extName = "png";
                break;
            case "image/x-png":
                extName = "png";
                break;
        }
        if (extName.length == 0) {
            res.locals.error = "pngjpg";
            res.render("index", {title: TITLE});
            return;
        }

        allFile.forEach(function(file,index){
            var fieldName=file[0];
            var types = file[1].name.split(".");
            var date = new Date();
            var ms = Date.parse(date);
            fs.renameSync(file[1].path,form.uploadDir+"/"+types[0]+"."+String(types[types.length-1]));//
        });
    }
    })
    
    

the following is the printout:

filed::: file
file::: File {
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  size: 0,
  path: "public\\avatar\\upload_255fc288f22bda9c2e6f3f985c353609",
  name: "",
  type: "application/octet-stream",
  hash: null,
  lastModifiedDate: null,
  _writeStream:
   WriteStream {
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: true,
        needDrain: false,
        ending: true,
        ended: true,
        finished: true,
        destroyed: true,
        decodeStrings: true,
        defaultEncoding: "utf8",
        length: 0,
        writing: false,
        corked: 0,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: true,
        errorEmitted: false,
        emitClose: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     _events: { open: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     path: "public\\avatar\\upload_255fc288f22bda9c2e6f3f985c353609",
     fd: null,
     flags: "w",
     mode: 438,
     start: undefined,
     autoClose: true,
     pos: undefined,
     bytesWritten: 0,
     closed: false } }
    filed::: file
    file::: File {
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  size: 16144,
  path:
   "public\\avatar\\upload_5427e66c7f095253103cf435ca326903.png",
  name: "1506492549107.png",
  type: "image/png",
  hash: null,
  lastModifiedDate: 2018-09-06T07:32:45.571Z,
  _writeStream:
   WriteStream {
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: true,
        needDrain: false,
        ending: true,
        ended: true,
        finished: true,
        destroyed: true,
        decodeStrings: true,
        defaultEncoding: "utf8",
        length: 0,
        writing: false,
        corked: 0,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: true,
        errorEmitted: false,
        emitClose: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     _events: {},
     _eventsCount: 0,
     _maxListeners: undefined,
     path:
      "public\\avatar\\upload_5427e66c7f095253103cf435ca326903.png",
     fd: null,
     flags: "w",
     mode: 438,
     start: undefined,
     autoClose: true,
     pos: undefined,
     bytesWritten: 16144,
     closed: false } }
filed::: file
file::: File {
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  size: 18898,
  path:
   "public\\avatar\\upload_75052b6e88a8a2a04c3e98090b577dfd.png",
  name: "1506492173800.png",
  type: "image/png",
  hash: null,
  lastModifiedDate: 2018-09-06T07:32:45.575Z,
  _writeStream:
   WriteStream {
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: true,
        needDrain: true,
        ending: true,
        ended: true,
        finished: true,
        destroyed: true,
        decodeStrings: true,
        defaultEncoding: "utf8",
        length: 0,
        writing: false,
        corked: 0,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: true,
        errorEmitted: false,
        emitClose: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: false,
     _events: {},
     _eventsCount: 0,
     _maxListeners: undefined,
     path:
      "public\\avatar\\upload_75052b6e88a8a2a04c3e98090b577dfd.png",
     fd: null,
     flags: "w",
     mode: 438,
     start: undefined,
     autoClose: true,
     pos: undefined,
     bytesWritten: 18898,
     closed: false } }
< H2 > I wonder if anyone has ever met? I really don"t know what"s going on. Ask for advice. < / H2 >
May.29,2021

the first parameter filed seems to be defined on the official website as name : https://www.npmjs.com/package.

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e608b2-44395.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e608b2-44395.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?