Add upload api for files

This commit is contained in:
Rene Pfeuffer
2019-08-30 09:11:14 +02:00
parent 3541ae5e63
commit 23d1a0a18b

View File

@@ -54,17 +54,15 @@ class ApiClient {
return this.httpRequestWithJSONBody("POST", url, contentType, payload); return this.httpRequestWithJSONBody("POST", url, contentType, payload);
} }
postBinary(contentType: string, url: string, files: File[]) { postBinary(url: string, fileAppender: FormData => void) {
let formData = new FormData(); let formData = new FormData();
for (let i = 0; i < files.length; i++) { fileAppender(formData);
formData.append("file" + i, files[i]);
}
let options: RequestOptions = { let options: RequestOptions = {
method: "POST", method: "POST",
body: formData body: formData
}; };
return this.httpRequestWithBinaryBody(options, contentType, url); return this.httpRequestWithBinaryBody(options, url);
} }
put(url: string, payload: any, contentType: string = "application/json") { put(url: string, payload: any, contentType: string = "application/json") {
@@ -97,13 +95,15 @@ class ApiClient {
method: method, method: method,
body: JSON.stringify(payload) 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); options = Object.assign(options, fetchOptions);
if (contentType) {
// $FlowFixMe // $FlowFixMe
options.headers["Content-Type"] = contentType; options.headers["Content-Type"] = contentType;
}
return fetch(createUrl(url), options).then(handleFailure); return fetch(createUrl(url), options).then(handleFailure);
} }