From d980448e1b729eee820371386eb2cd083772dc5b Mon Sep 17 00:00:00 2001 From: soupette Date: Wed, 25 Mar 2020 17:07:12 +0100 Subject: [PATCH] Add download option Signed-off-by: soupette --- .../admin/src/components/EditForm/index.js | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/strapi-plugin-upload/admin/src/components/EditForm/index.js b/packages/strapi-plugin-upload/admin/src/components/EditForm/index.js index 03b8eed37d..4ba98d88ea 100644 --- a/packages/strapi-plugin-upload/admin/src/components/EditForm/index.js +++ b/packages/strapi-plugin-upload/admin/src/components/EditForm/index.js @@ -1,3 +1,4 @@ +/* eslint-disable jsx-a11y/anchor-is-valid */ /* eslint-disable react/jsx-fragments */ import React, { Fragment, @@ -8,6 +9,7 @@ import React, { useImperativeHandle, } from 'react'; import { CopyToClipboard } from 'react-copy-to-clipboard'; +import axios from 'axios'; import { get } from 'lodash'; import PropTypes from 'prop-types'; import { Inputs } from '@buffetjs/custom'; @@ -205,7 +207,22 @@ const EditForm = forwardRef( }; const handleClickDownload = () => { - aRef.current.click(); + axios + .get(prefixedFileURL, { + headers: new Headers({ Origin: window.location.origin, mode: 'cors' }), + responseType: 'blob', + }) + .then(({ data }) => { + const blobUrl = URL.createObjectURL(data); + + aRef.current.download = downloadFileName; + aRef.current.href = blobUrl; + + aRef.current.click(); + }) + .catch(err => { + console.error(err); + }); }; const handleSubmit = e => { @@ -242,17 +259,14 @@ const EditForm = forwardRef( type="download" onClick={handleClickDownload} /> - {isFileDownloadable && ( - - hidden - - )} + + hidden + +