perf: Downloading files does not trigger the beforeunload event

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

View File

@@ -49,6 +49,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',
@@ -201,10 +202,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 {
@@ -230,10 +230,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

@@ -217,13 +217,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',
@@ -134,10 +135,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)
} }
} }
}, },