From 7f7c56cbb4ecf1d3f3657283cbe7de2637504fff Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 10 Jan 2022 02:19:29 -0600 Subject: Copied code from old UI in order to fix downloads in mobile app --- modern/src/reports/StopReportPage.js | 37 +++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'modern/src/reports/StopReportPage.js') diff --git a/modern/src/reports/StopReportPage.js b/modern/src/reports/StopReportPage.js index ab1f299..5012637 100644 --- a/modern/src/reports/StopReportPage.js +++ b/modern/src/reports/StopReportPage.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useRef } from 'react'; import { DataGrid } from '@material-ui/data-grid'; import { useTheme } from '@material-ui/core/styles'; import { @@ -10,6 +10,9 @@ import { useAttributePreference } from '../common/preferences'; import { useTranslation } from '../LocalizationProvider'; const Filter = ({ setItems }) => { + const inputElement = useRef(); + const [data, setData] = useState({url: '', filename: ''}); + const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail, @@ -21,13 +24,41 @@ const Filter = ({ setItems }) => { if (contentType === 'application/json') { setItems(await response.json()); } else { - window.location.assign(window.URL.createObjectURL(await response.blob())); + // Copied from /web/app/view/ReportController.js (but without Ext) + const disposition = response.headers.get('content-disposition'); + const filename = disposition.slice(disposition.indexOf('=') + 1, disposition.length); + const blob = new Blob([await response.blob()], { type: contentType }); + if (typeof window.navigator.msSaveBlob !== 'undefined') { + // IE workaround + window.navigator.msSaveBlob(blob, filename); + } else { + const url = window.URL || window.webkitURL; + const downloadUrl = url.createObjectURL(blob); + if (filename) { + setData({url: downloadUrl, filename: filename}); + setTimeout(() => { + inputElement.current.click(); + }, 100); + } + setTimeout(() => { + url.revokeObjectURL(downloadUrl); + }, 100); + } } } } }; - return ; + return ( + <> + + + + ); }; const StopReportPage = () => { -- cgit v1.2.3