From 6efc375112fdef5bd58fe62e26a8ba907b7736fb Mon Sep 17 00:00:00 2001 From: Deepak Jose Date: Tue, 29 Oct 2024 17:15:35 +0530 Subject: [PATCH] Fixes CORS error when downloading attachments. (#1254) * Refactored the download util to directly use the file url instead of fetching the file content using axios. * Removed the unused file download api file. --- src/apis/file_download.js | 9 --------- src/components/Attachments/utils.js | 12 +++--------- 2 files changed, 3 insertions(+), 18 deletions(-) delete mode 100644 src/apis/file_download.js diff --git a/src/apis/file_download.js b/src/apis/file_download.js deleted file mode 100644 index 85aa960e..00000000 --- a/src/apis/file_download.js +++ /dev/null @@ -1,9 +0,0 @@ -import axios from "axios"; - -const newAxiosInstance = axios.create(); - -const getFile = url => newAxiosInstance.get(url, { responseType: "blob" }); - -const fileDownloadApi = { getFile }; - -export default fileDownloadApi; diff --git a/src/components/Attachments/utils.js b/src/components/Attachments/utils.js index 54681c95..81b4b1c7 100644 --- a/src/components/Attachments/utils.js +++ b/src/components/Attachments/utils.js @@ -1,29 +1,23 @@ import { Toastr } from "neetoui"; import { mergeRight } from "ramda"; -import fileDownloadApi from "apis/file_download"; - import { DEFAULT_FILE_UPLOAD_CONFIG } from "./constants"; export const buildFileUploadConfig = config => mergeRight(DEFAULT_FILE_UPLOAD_CONFIG, config); -export const downloadFile = async (fileUrl, filename) => { +export const downloadFile = (fileUrl, filename) => { try { - const response = await fileDownloadApi.getFile(fileUrl); - const blob = new Blob([response.data]); - const url = URL.createObjectURL(blob); - const a = document.createElement("a"); - a.href = url; + a.href = fileUrl; a.setAttribute("download", filename); + a.setAttribute("target", "_blank"); a.style.display = "none"; document.body.appendChild(a); a.click(); - URL.revokeObjectURL(url); document.body.removeChild(a); } catch (error) { Toastr.error(error);