Skip to content

Commit 71d20d9

Browse files
authored
Merge pull request #21 from kit-data-manager/zip_response
feat: added blob response instead of json and added waiting curser(spin), alert when mapping fails, file name changes
2 parents 8313016 + a746ddb commit 71d20d9

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/main.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -165,35 +165,47 @@ class MappingInputProvider extends HTMLElement {
165165
if (uploadedFile != null) {
166166
const execUrl = this.baseUrl.toString() + "api/v1/mappingExecution/" + selectedMappingId;
167167
const file = uploadedFile.file;
168-
169168
let formData = new FormData();
170169
if (file != undefined) {
171170
formData.append("document", file);
172171
}
173172
return fetch(execUrl, {
174173
method: "POST",
175-
body: formData
176-
}).then(response => response.json())
177-
.then(responseJson => {
174+
body: formData,
175+
}).then(response => {
176+
if (response.status !== 200) {
177+
throw new Error("Request failed with status " + response.status);
178+
}
179+
const contentDisposition = response.headers.get("content-disposition") || "";
180+
const contentType = response.headers.get("content-type") || "";
181+
return Promise.all([response.blob(), contentDisposition, contentType]);
182+
})
183+
.then(([responseBlob, contentDisposition, contentType]) => {
178184
if (download) {
179-
this.triggerDownload(responseJson);
185+
this.triggerDownload(responseBlob, contentDisposition, contentType);
180186
}
181-
})
187+
}).catch(error => {
188+
console.error("Error occured due to response other than 200:", error);
189+
alert("A remote mapping error occured. Please check server logs for details.");
190+
});
182191
}
183192
}
184193
}
185194

186195
/**
187196
* In case if download is required triggerDownload can be used
188197
*/
189-
triggerDownload(response: Promise<any>) {
198+
triggerDownload(response: Blob, contentDisposition: string, contentType: string) {
190199
const element = document.createElement('a');
191-
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(response)));
192-
element.setAttribute('download', "result.json");
200+
const filename = contentDisposition.substr(contentDisposition.lastIndexOf("=") + 1) || 'result';
201+
element.type = contentType;
202+
element.href = URL.createObjectURL(response);
203+
element.download = filename;
193204
element.style.display = 'none';
194205
this.shadowRoot.appendChild(element);
195206
element.click();
196207
this.shadowRoot.removeChild(element);
208+
URL.revokeObjectURL(element.href);
197209
}
198210
}
199211

0 commit comments

Comments
 (0)