From 23d1a0a18b608e8b5540135f44add588f6f79e5e Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Fri, 30 Aug 2019 09:11:14 +0200 Subject: [PATCH] Add upload api for files --- .../packages/ui-components/src/apiclient.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scm-ui-components/packages/ui-components/src/apiclient.js b/scm-ui-components/packages/ui-components/src/apiclient.js index 0f2ae69ffa..80c2c4dbfa 100644 --- a/scm-ui-components/packages/ui-components/src/apiclient.js +++ b/scm-ui-components/packages/ui-components/src/apiclient.js @@ -54,17 +54,15 @@ class ApiClient { return this.httpRequestWithJSONBody("POST", url, contentType, payload); } - postBinary(contentType: string, url: string, files: File[]) { + postBinary(url: string, fileAppender: FormData => void) { let formData = new FormData(); - for (let i = 0; i < files.length; i++) { - formData.append("file" + i, files[i]); - } + fileAppender(formData); let options: RequestOptions = { method: "POST", body: formData }; - return this.httpRequestWithBinaryBody(options, contentType, url); + return this.httpRequestWithBinaryBody(options, url); } put(url: string, payload: any, contentType: string = "application/json") { @@ -97,13 +95,15 @@ class ApiClient { method: method, body: JSON.stringify(payload) }; - return this.httpRequestWithBinaryBody(options, contentType, url); + return this.httpRequestWithBinaryBody(options, url, contentType); } - httpRequestWithBinaryBody(options: RequestOptions, contentType: string, url: string) { + httpRequestWithBinaryBody(options: RequestOptions, url: string, contentType?: string) { options = Object.assign(options, fetchOptions); - // $FlowFixMe - options.headers["Content-Type"] = contentType; + if (contentType) { + // $FlowFixMe + options.headers["Content-Type"] = contentType; + } return fetch(createUrl(url), options).then(handleFailure); }