perf: Downloading files does not trigger the beforeunload event

This commit is contained in:
feng 2024-07-16 12:34:18 +08:00
parent da09af79a7
commit 00bafa8164
4 changed files with 13 additions and 14 deletions

View File

@ -48,6 +48,7 @@
import Dialog from '@/components/Dialog/index.vue' import Dialog from '@/components/Dialog/index.vue'
import { createSourceIdCache } from '@/api/common' import { createSourceIdCache } from '@/api/common'
import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query' import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query'
import { download } from '@/utils/common'
export default { export default {
name: 'ExportDialog', name: 'ExportDialog',
@ -187,10 +188,7 @@ export default {
}) })
}, },
downloadCsv(url) { downloadCsv(url) {
const a = document.createElement('a') download(url)
a.href = url
a.click()
window.URL.revokeObjectURL(url)
}, },
async defaultPerformExport(selectRows, exportOption, q, exportTypeOption) { async defaultPerformExport(selectRows, exportOption, q, exportTypeOption) {
const url = (process.env.VUE_APP_ENV === 'production') ? (`${this.url}`) : (`${process.env.VUE_APP_BASE_API}${this.url}`) const url = (process.env.VUE_APP_ENV === 'production') ? (`${this.url}`) : (`${process.env.VUE_APP_BASE_API}${this.url}`)

View File

@ -68,7 +68,7 @@
<script> <script>
import Dialog from '@/components/Dialog/index.vue' import Dialog from '@/components/Dialog/index.vue'
import ImportTable from '@/components/Table/ListTable/TableAction/ImportTable.vue' import ImportTable from '@/components/Table/ListTable/TableAction/ImportTable.vue'
import { getErrorResponseMsg } from '@/utils/common' import { download, getErrorResponseMsg } from '@/utils/common'
import { createSourceIdCache } from '@/api/common' import { createSourceIdCache } from '@/api/common'
export default { export default {
@ -221,10 +221,7 @@ export default {
this.$message.success(msg) this.$message.success(msg)
}, },
downloadCsv(url) { downloadCsv(url) {
const a = document.createElement('a') download(url)
a.href = url
a.click()
window.URL.revokeObjectURL(url)
}, },
async handleImportConfirm() { async handleImportConfirm() {
await this.$refs['importTable'].performUpload() await this.$refs['importTable'].performUpload()

View File

@ -363,13 +363,19 @@ export function downloadText(content, filename) {
} }
export function download(downloadUrl, filename) { export function download(downloadUrl, filename) {
const iframe = document.createElement('iframe')
iframe.style.display = 'none'
document.body.appendChild(iframe)
const a = document.createElement('a') const a = document.createElement('a')
a.href = downloadUrl a.href = downloadUrl
if (filename) { if (filename) {
a.download = filename a.download = filename
} }
iframe.contentWindow.document.body.appendChild(a)
a.click() a.click()
window.URL.revokeObjectURL(downloadUrl) setTimeout(() => {
document.body.removeChild(iframe)
}, 1000 * 60 * 30) // If you can't download it in half an hour, don't download it.
} }
export function diffObject(object, base) { export function diffObject(object, base) {

View File

@ -22,6 +22,7 @@ import isFalsey from '@/components/Table/DataTable/compenents/el-data-table/util
import deepmerge from 'deepmerge' import deepmerge from 'deepmerge'
import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query' import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query'
import { createSourceIdCache } from '@/api/common' import { createSourceIdCache } from '@/api/common'
import { download } from '@/utils/common'
export default { export default {
name: 'CommandList', name: 'CommandList',
@ -144,10 +145,7 @@ export default {
queryUtil.stringify(query, '=', '&') queryUtil.stringify(query, '=', '&')
url = url + queryStr url = url + queryStr
this.$log.debug('Export url: ', this.url, '=>', url) this.$log.debug('Export url: ', this.url, '=>', url)
const a = document.createElement('a') download(url + queryStr)
a.href = url
a.click()
window.URL.revokeObjectURL(url + queryStr)
} }
} }
}, },