1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
import axios from 'axios';
export default async function fileDownload(url, data, fileName) { axios.defaults.headers.post['Content-Type'] = 'application/json'; await axios({ method: 'post', url: url, data: JSON.stringify(data), responseType: 'blob' }).then((res) => { const content = res.data; const contentType = res.headers['content-type'] || "application/octet-stream"; const blob = new Blob([content], {type: contentType}); if ('download' in document.createElement('a')) { const fileLink = document.createElement('a'); fileLink.download = fileName; fileLink.style.display = 'none'; fileLink.href = URL.createObjectURL(blob); document.body.appendChild(fileLink); fileLink.click(); URL.revokeObjectURL(fileLink.href); document.body.removeChild(fileLink); } else { navigator.msSaveBlob(blob, fileName); } }) }
import fileDownload from 'fileDownload'; fileDownload("url", "data", "fileName").then( () => { })
export default async function pdfPreview(method, url, data, fileName) { let open; if ('download' in document.createElement('a')) { open = window.open(); } axios.defaults.headers.post['Content-Type'] = 'application/json'; try { await axios({ method: method, url: url, data: JSON.stringify(data), responseType: 'blob' }).then((res) => { const content = res.data; const contentType = res.headers['content-type'] || "application/pdf"; const blob = new Blob([content], {type: contentType}); if ('download' in document.createElement('a')) { const href = URL.createObjectURL(blob); open.location = href; } else { navigator.msSaveBlob(blob, fileName); } }) } catch(err) { } }
|