From 55e8d2b1f5b7b646063dc27e49d70c42a2cfe789 Mon Sep 17 00:00:00 2001 From: iventura Date: Thu, 2 May 2024 16:09:45 -0700 Subject: [PATCH] CSCwj84515: only download file when response returns zip file - and fixed file name --- src/components/Operation/Operation.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/Operation/Operation.tsx b/src/components/Operation/Operation.tsx index 6402f4136f..c65d2c4833 100644 --- a/src/components/Operation/Operation.tsx +++ b/src/components/Operation/Operation.tsx @@ -163,21 +163,24 @@ export class Operation extends React.Component { setCookieParams(cookieParams); this.setState({ pendingRequest: true }); - fetch(`${appendParamsToPath(path, pathParams, queryParams)}`, request) + const api_url = `${appendParamsToPath(path, pathParams, queryParams)}` + fetch(api_url, request) .then(async (response: any) => { const statusCode = response.status; const statusType = mapStatusCodeToType(statusCode); const isError = statusType === 'error'; const contentType = response.headers.get('content-type'); - if (contentType?.indexOf('application/zip') !== -1 && !isError) { - const slug = last(path?.split('/') || []); + if (contentType && contentType?.indexOf('application/zip') !== -1 && !isError) { + const slug = last(api_url?.split('/') || []); const fileName = slug || 'file'; const bytes = await response.blob(); + const temporaryAnchor = document.createElement('a'); temporaryAnchor.href = URL.createObjectURL(bytes); temporaryAnchor.setAttribute('download', fileName); temporaryAnchor.click(); + this.setState({ response: { type: mapStatusCodeToType(statusCode),