Formdata uploads multiple images, why can't it traverse

for(var _img in img_file){           
    formdata.append("file", img_file[_img]);
}
let files = ctx.request.files;
console.log("filesPP+:");console.log(files);
    for(let index in files){
        console.log(index);//file[]
        file = files[index];
        console.log("file---:")
        console.log(file)

console.log (index); / / output file []
and files and files [index] are the same thing
Why?

Apr.03,2021

FormData cannot pass the instance's [] or. Attributes can only be obtained through get
such as

let form = new FormData()
form.append('prop', 1)
console.log(form.prop)//undefined
console.log(form.get('prop'))

if you need to get all the fields, get a pseudo array through api such as keys,values and then manipulate


solved

function upload(files,title){  
    let imgList=[],file,url;
    
    files = files['file'];//
    
    if(files.length){//
        for(let index in files){
            file = files[index];     

            url=`uploads/${title}-${file.name}`;
            fs.createReadStream(file.path).pipe(fs.createWriteStream(url))
        
            imgList.push([index,url]);
        }
    }else{   
        file = files;        
        url=`uploads/${title}-${file.name}`;
        fs.createReadStream(file.path).pipe(fs.createWriteStream(url))
        imgList.push([0,url]);
    }
     
     return imgList;
   
}
exports.add = async(ctx, next)=>{
    let {title,body,tags,category}=ctx.request.body;
    let files = ctx.request.files;//

    let imgList = upload(files,title);

    let post = new Post({
        title,
        body,
        tags,
        category,
        imgList
    })
    let result= await dbHelper.Save(post);
    ctx.response.body = result;    
}
Menu