From 01ecd144e1cb24cadb1b75727c740168ccd42947 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Thu, 29 Aug 2019 16:37:57 +0200 Subject: [PATCH] Add method to upload files to api client --- .../packages/ui-components/src/apiclient.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scm-ui-components/packages/ui-components/src/apiclient.js b/scm-ui-components/packages/ui-components/src/apiclient.js index 551bb3e2eb..0f2ae69ffa 100644 --- a/scm-ui-components/packages/ui-components/src/apiclient.js +++ b/scm-ui-components/packages/ui-components/src/apiclient.js @@ -54,6 +54,19 @@ class ApiClient { return this.httpRequestWithJSONBody("POST", url, contentType, payload); } + postBinary(contentType: string, url: string, files: File[]) { + let formData = new FormData(); + for (let i = 0; i < files.length; i++) { + formData.append("file" + i, files[i]); + } + + let options: RequestOptions = { + method: "POST", + body: formData + }; + return this.httpRequestWithBinaryBody(options, contentType, url); + } + put(url: string, payload: any, contentType: string = "application/json") { return this.httpRequestWithJSONBody("PUT", url, contentType, payload); } @@ -84,6 +97,10 @@ class ApiClient { method: method, body: JSON.stringify(payload) }; + return this.httpRequestWithBinaryBody(options, contentType, url); + } + + httpRequestWithBinaryBody(options: RequestOptions, contentType: string, url: string) { options = Object.assign(options, fetchOptions); // $FlowFixMe options.headers["Content-Type"] = contentType;