如何在angular2 +和NodeJS

时间:2018-12-21 12:46:41

标签: node.js angular file append

我准备了一个表单,该表单在angular2中接受三个值和一个文件作为输入,并且能够接受文件作为输入并将其附加到formData。但是,我无法将formData与其他值附加在一起。即服务器端存在的我的fileUpload函数-NodeJS不能识别此数据。我的意思是我不知道如何在server.js文件中检索其数据。

角度代码-

app.component.ts

 filesToUpload: Array<File> = [];

 upload(value) {


const formData: any = new FormData();
const files: Array<File> = this.filesToUpload;
var data = [];

console.log("inside upload, knowledge of the file =", files);



var checkFileNameTrue = value.addFile;

console.log(" inside upload, this.checkFileName = ", checkFileNameTrue );
this.checkFileName = files[0].name;

console.log(" inside upload, this.checkFileName = ", this.checkFileName);


}


  for (let i = 0; i < files.length; i++) {
    formData.append("uploads[]", files[i], files[i]['name']);



    console.log("inside upload, formData = ", formData);
  }



console.log("files.length = ", files.length);

console.log("value.addOrganisation = ", value.addOrganisation)
var length = files.length
    formData.append("uploads[]", data[0], value.addOrganisation);
    formData.append("uploads[]", data[1], value.addDepartment);
    formData.append("uploads[]", data[2], value.addSystem);



  this.wsdlValue = {
    'formData': formData,
    'addOrganisation': value.addOrganisation,
    'addDepartment': value.addDepartment,
    'addSystem': value.addSystem,

  };

  console.log("this.wsdlValue = ", formData)

  this.http.post('http://localhost:3003/upload', formData)
    .map(files => files)
    .subscribe(files => {


      console.log('files', files)

      this.uploadFileName = files[0].filename;
      this.uploadPath = files[0].path;

      this.shareData.uploadedFiles.push(this.uploadFileName);

      this.shareData.uploadedPaths.push(this.uploadPath);



    })

}

}

nodeJs代码

server.js

//code

var upload = multer({ storage: storage });



app.post("/upload", upload.array("uploads[]", 12), function (req, res) {

console.log("request = ", req)

    fileName = req.files[0].path;
    organisation = req.data[0];
    department = req.data[1];
    system = req.data[2];

    console.log("organisation =", organisation , " department = ", department, " system = ", system)



        myModuleYaml.defineRoutes(router, fileName);
    console.log("global variable FileName = ", fileName)


            res.send(req.files);
});

server.js的输出-

 TypeError: Cannot read property '0' of undefined

 Line -  fileName = req.files[0].path;

0 个答案:

没有答案