Skip to content

Instantly share code, notes, and snippets.

@fakiolinho
Last active November 4, 2019 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakiolinho/46ea2ae9708b4882ee5e39f3dd93af4a to your computer and use it in GitHub Desktop.
Save fakiolinho/46ea2ae9708b4882ee5e39f3dd93af4a to your computer and use it in GitHub Desktop.
import fileSaver from 'utils/fileSaver';
import fileReader from 'utils/fileReader';
import { downloadFileRequest } from './api';
import * as types from './actionTypes';
export const downloadFile = id => async dispatch => {
try {
dispatch({
type: types.DOWNLOAD_FILE_REQUEST,
});
const { data } = await downloadFileRequest(id);
await fileSaver(data, 'fileName.txt');
dispatch({
type: types.DOWNLOAD_FILE_SUCCESS,
});
alert('File downloaded successfully!');
} catch (error) {
dispatch({
type: types.DOWNLOAD_FILE_ERROR,
});
if (error.response) {
try {
const { data } = error.response;
// Read file
const file = await fileReader(data);
// Parse content and retrieve 'message'
const { message } = JSON.parse(file);
alert(message);
} catch (readError) {
// Show a dummy error if sth goes wrong while retrieving 'message'
alert('Something went wrong while downloading this file');
}
} else {
alert('Something went wrong while downloading this file');
}
}
};
@lobmails
Copy link

lobmails commented Nov 4, 2019

Hi, can you please share the file 'actionTypes' referred in action.js:

Or if you can share the link to the repo, that'll be a great help.

import * as types from './actionTypes';

Please refer: https://stackoverflow.com/questions/58687519/how-to-upload-a-csv-file-to-blob-storage-using-axios-fetch-nodejs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment