From a4ee7cc83e63273a45afc765cc06ac2a1b4423bf Mon Sep 17 00:00:00 2001 From: Richard Park Date: Tue, 5 Nov 2019 13:39:28 +0900 Subject: [PATCH] encodeFormData is added --- .../src/lib/utils/parameter.util.ts | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/projects/ucap-webmessenger-api/src/lib/utils/parameter.util.ts b/projects/ucap-webmessenger-api/src/lib/utils/parameter.util.ts index 96c8602e..ab29f31e 100644 --- a/projects/ucap-webmessenger-api/src/lib/utils/parameter.util.ts +++ b/projects/ucap-webmessenger-api/src/lib/utils/parameter.util.ts @@ -14,14 +14,50 @@ export class ParameterUtil { if (!param.hasOwnProperty(key)) { return; } - if (!!param[key]) { - parameter[parameterMap[key]] = param[key]; - } + + let v: any; if (!!extraParam && !!extraParam[key]) { - parameter[parameterMap[key]] = extraParam[key]; + v = extraParam[key]; + } else if (!!param[key]) { + v = param[key]; + } + + if (!!v) { + parameter[parameterMap[key]] = v; } }); return 0 === Object.keys(parameter).length ? null : parameter; } + + public static encodeFormData( + parameterMap: {}, + param: {}, + extraParam?: {} + ): FormData { + const formData: FormData = new FormData(); + + Object.keys(parameterMap).map(key => { + if (!param.hasOwnProperty(key)) { + return; + } + + let v: any; + if (!!extraParam && !!extraParam[key]) { + v = extraParam[key]; + } else if (!!param[key]) { + v = param[key]; + } + + if (!!v) { + if (v instanceof File) { + formData.append(parameterMap[key], v, v.name); + } else { + formData.append(parameterMap[key], v); + } + } + }); + + return formData; + } }