I am trying to download docx or pdf file in React JS(depending on server response) when I get the response from the server. Backend is implemented in Python/Flask.
When I try to download the file, it downloads but damaged and corrupted.
React JS
function downloadDocument(downloadLink, downloadType) {
const documentBlob = new Blob([downloadLink], { type: downloadType });
const url = window.URL.createObjectURL(documentBlob);
const anchor = document.createElement('a');
anchor.href = url;
anchor.download = document_title;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
window.URL.revokeObjectURL(url) ;
}
Backend Python/Flask
@app.route('/download_document_docx/<request_document_id>', methods=['GET'])
@token_required
def download_document_docx(current_user, request_document_id):
'''
Docx document preparation code
'''
return send_file(docx_file_path, as_attachment=True, mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document")