Skip to content

Commit

Permalink
Fixes CORS error when downloading attachments. (#1254)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
deepakjosp authored Oct 29, 2024
1 parent da7373f commit 6efc375
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 18 deletions.
9 changes: 0 additions & 9 deletions src/apis/file_download.js

This file was deleted.

12 changes: 3 additions & 9 deletions src/components/Attachments/utils.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit 6efc375

Please sign in to comment.