CF2-8: Добавлено скачивание репортов

This commit is contained in:
KharchenkoVV 2021-06-09 17:25:54 +05:00
parent a89c5200d7
commit e4d4d044d6

View File

@ -37,11 +37,6 @@ const imgPaths = {
'.las': '/images/las.png'
}
const contentTypes = {
'.pdf': 'application/pdf',
'.las': 'application/octet-stream'
}
// Экспорт рендера
export default function Report(props) {
@ -85,8 +80,8 @@ export default function Report(props) {
<br />
<span> { progressData.operation } </span>
<br />
<a onClick={event => getReportFile(event, progressData.reportName)}
download>
<a onClick={event => {getReportFile(event, progressData.reportName)}}
download={progressData.reportName}>
{ progressData.reportName }
</a>
</>
@ -94,17 +89,31 @@ export default function Report(props) {
}
const getReportFile = async (event, reportFileName) => {
try {
const element = event.target
let bytesArray = await ReportService.getReport(wellId, reportFileName)
const contentType = contentTypes[reportFileName.slice(-4)]
let reportUrl = URL.createObjectURL(new File(bytesArray, {type: contentType}))
element.href = reportUrl
} catch (error) {
notify(`Не удалось скачать отчет по скважине (${wellId}) c
${rangeDate[0].format("DD.MM.YYYY hh:mm:ss")} по
${rangeDate[1].format("DD.MM.YYYY hh:mm:ss")}`, 'error')
console.log(error)
const element = event.target
if(!element.href.length) {
try {
await fetch(`/api/report/${wellId}/${reportFileName}`, {
headers: {
Authorization: 'Bearer ' + localStorage['token']
}
})
.then(async (response) => {
const blob = await response.blob();
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = function (e) {
element.href = e.target.result
element.click()
};
});
} catch (error) {
notify(`Не удалось скачать отчет по скважине (${wellId}) c
${rangeDate[0].format("DD.MM.YYYY hh:mm:ss")} по
${rangeDate[1].format("DD.MM.YYYY hh:mm:ss")}`, 'error')
console.log(error)
}
}
}