Skip to content

Commit

Permalink
CSCwj84515: only download file when response returns zip file - and f…
Browse files Browse the repository at this point in the history
…ixed file name
  • Loading branch information
iventura committed May 2, 2024
1 parent afc1857 commit 55e8d2b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/components/Operation/Operation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,24 @@ export class Operation extends React.Component<OperationProps, OperationState> {
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),
Expand Down

0 comments on commit 55e8d2b

Please sign in to comment.