Skip to content

Commit

Permalink
Merge pull request #4222 from jumpserver/pr@dev@download
Browse files Browse the repository at this point in the history
perf: Downloading files does not trigger the beforeunload event
  • Loading branch information
feng626 committed Jul 16, 2024
2 parents 34effdb + bf4d8ce commit 453c4b1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/components/Table/ListTable/TableAction/ExportDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import Dialog from '@/components/Dialog/index.vue'
import { createSourceIdCache } from '@/api/common'
import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query'
import { download } from '@/utils/common'
export default {
name: 'ExportDialog',
Expand Down Expand Up @@ -201,10 +202,7 @@ export default {
})
},
downloadCsv(url) {
const a = document.createElement('a')
a.href = url
a.click()
window.URL.revokeObjectURL(url)
download(url)
},
async defaultPerformExport(selectRows, exportOption, q, exportTypeOption) {
const url = (process.env.VUE_APP_ENV === 'production') ? (`${this.url}`) : (`${process.env.VUE_APP_BASE_API}${this.url}`)
Expand Down
7 changes: 2 additions & 5 deletions src/components/Table/ListTable/TableAction/ImportDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<script>
import Dialog from '@/components/Dialog/index.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'
export default {
Expand Down Expand Up @@ -230,10 +230,7 @@ export default {
this.$message.success(msg)
},
downloadCsv(url) {
const a = document.createElement('a')
a.href = url
a.click()
window.URL.revokeObjectURL(url)
download(url)
},
async handleImportConfirm() {
await this.$refs['importTable'].performUpload()
Expand Down
8 changes: 7 additions & 1 deletion src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,19 @@ export function downloadText(content, filename) {
}

export function download(downloadUrl, filename) {
const iframe = document.createElement('iframe')
iframe.style.display = 'none'
document.body.appendChild(iframe)
const a = document.createElement('a')
a.href = downloadUrl
if (filename) {
a.download = filename
}
iframe.contentWindow.document.body.appendChild(a)
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) {
Expand Down
6 changes: 2 additions & 4 deletions src/views/sessions/CommandList/BaseList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import isFalsey from '@/components/Table/DataTable/compenents/el-data-table/util
import deepmerge from 'deepmerge'
import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query'
import { createSourceIdCache } from '@/api/common'
import { download } from '@/utils/common'
export default {
name: 'CommandList',
Expand Down Expand Up @@ -134,10 +135,7 @@ export default {
queryUtil.stringify(query, '=', '&')
url = url + queryStr
this.$log.debug('Export url: ', this.url, '=>', url)
const a = document.createElement('a')
a.href = url
a.click()
window.URL.revokeObjectURL(url + queryStr)
download(url + queryStr)
}
}
},
Expand Down

0 comments on commit 453c4b1

Please sign in to comment.