无法通过表单数据发布快递请求

时间:2019-12-25 14:04:58

标签: angular multer

我项目的当前体系结构是在客户端上我使用Angular,表示为中间件,而Java为后端服务。我需要将带有一些信息的角度页面中的文件发布到我的后端服务。在有角度的一面,我正在创建formdata并进行调用,如下所示

let url = '/api/commonCommand/save';
        let headers = new HttpHeaders();
        let options = {
            "headers": headers
        };
        console.log("OPTTIONS---->", options)
        console.log("body...",body)
        return this.http.post(url, body, options).pipe(map(res => {
            return res;
        }, err => {
            return err;
        }));

这里是

const formData: FormData = new FormData();
formData.append('file', files[0], files[0].name);  //files = this.fileElement.nativeElement.files;
formData.append('templatename', this.fileName);
if (this.selectedDropdownValue === 'configuration') {
  formData.append('type', 'zip');
} else {
  formData.append('type', 'json');
}
formData.append('filecategory', this.selectedDropdownValue);

在快递方面,我正在使用multer解析此表单数据,然后再次使用请求包进行发布调用,但此调用失败,并显示错误,如下所示

TypeError: Cannot read property 'name' of null
    at FormData._getContentDisposition (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\form-data\lib\form_data.js:226:40)
    at FormData._multiPartHeader (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\form-data\lib\form_data.js:177:33)
    at FormData.append (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\form-data\lib\form_data.js:70:21)
    at appendFormValue (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\request\request.js:326:21)
    at Request.init (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\request\request.js:337:11)
    at new Request (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\request\request.js:127:8)
    at request (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\request\index.js:53:10)
    at Function.post (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\request\index.js:61:12)
    at C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\dist\server\router\common.shared.route.js:204:23
    at Layer.handle [as handle_request] (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\layer.js:95:5)
    at C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:317:13)
    at C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Connectivity Framework\Web Application\ogd-edge-pipeline-management-web\node_modules\express\lib\router\index.js:335:12)

我在表达方面的要求如下所示

       let newFormData = new FormData();
       newFormData.append('file', request['files'][0].buffer, (request['files'][0].originalname).toString());
       newFormData.append('templatename', request.body.templatename)
       newFormData.append('type', request.body.type)
       newFormData.append('filecategory', request.body.filecategory)
       let url = "http://localhost:8087/v1/abc/template";
       requestObj.post({url: url, formData: newFormData, headers: request.headers}, function (err, resp) {
       if (!err && resp.statusCode == 200) {
            console.log('successfully fetched result for pipeline save ', resp);
            return response.status(200).json(resp);  
        }
        else {
            console.error('error in fetching result for pipeline save', err);
            if (err && err.error && err.error.errorCode) {
              return response.status(err.errorCode).json(err.error);
            } else {
               return response.status(500).json(err);
        }
            return err;
        }
    });

如何管理将这些数据发送到Java后端?另外,是否有可能我可以直接将从angular获取的有效载荷发送到java服务,而无需使用multer进行解析和再次定义formData?

0 个答案:

没有答案