Last active
November 4, 2019 23:49
-
-
Save fakiolinho/46ea2ae9708b4882ee5e39f3dd93af4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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