Skip to content

Commit 453c4b1

Browse files
authored
Merge pull request #4222 from jumpserver/pr@dev@download
perf: Downloading files does not trigger the beforeunload event
2 parents 34effdb + bf4d8ce commit 453c4b1

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

src/components/Table/ListTable/TableAction/ExportDialog.vue

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import Dialog from '@/components/Dialog/index.vue'
5050
import { createSourceIdCache } from '@/api/common'
5151
import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query'
52+
import { download } from '@/utils/common'
5253
5354
export default {
5455
name: 'ExportDialog',
@@ -201,10 +202,7 @@ export default {
201202
})
202203
},
203204
downloadCsv(url) {
204-
const a = document.createElement('a')
205-
a.href = url
206-
a.click()
207-
window.URL.revokeObjectURL(url)
205+
download(url)
208206
},
209207
async defaultPerformExport(selectRows, exportOption, q, exportTypeOption) {
210208
const url = (process.env.VUE_APP_ENV === 'production') ? (`${this.url}`) : (`${process.env.VUE_APP_BASE_API}${this.url}`)

src/components/Table/ListTable/TableAction/ImportDialog.vue

+2-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<script>
6969
import Dialog from '@/components/Dialog/index.vue'
7070
import ImportTable from '@/components/Table/ListTable/TableAction/ImportTable.vue'
71-
import { getErrorResponseMsg } from '@/utils/common'
71+
import { download, getErrorResponseMsg } from '@/utils/common'
7272
import { createSourceIdCache } from '@/api/common'
7373
7474
export default {
@@ -230,10 +230,7 @@ export default {
230230
this.$message.success(msg)
231231
},
232232
downloadCsv(url) {
233-
const a = document.createElement('a')
234-
a.href = url
235-
a.click()
236-
window.URL.revokeObjectURL(url)
233+
download(url)
237234
},
238235
async handleImportConfirm() {
239236
await this.$refs['importTable'].performUpload()

src/utils/common.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,19 @@ export function downloadText(content, filename) {
217217
}
218218

219219
export function download(downloadUrl, filename) {
220+
const iframe = document.createElement('iframe')
221+
iframe.style.display = 'none'
222+
document.body.appendChild(iframe)
220223
const a = document.createElement('a')
221224
a.href = downloadUrl
222225
if (filename) {
223226
a.download = filename
224227
}
228+
iframe.contentWindow.document.body.appendChild(a)
225229
a.click()
226-
window.URL.revokeObjectURL(downloadUrl)
230+
setTimeout(() => {
231+
document.body.removeChild(iframe)
232+
}, 1000 * 60 * 30) // If you can't download it in half an hour, don't download it.
227233
}
228234

229235
export function diffObject(object, base) {

src/views/sessions/CommandList/BaseList.vue

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import isFalsey from '@/components/Table/DataTable/compenents/el-data-table/util
2222
import deepmerge from 'deepmerge'
2323
import * as queryUtil from '@/components/Table/DataTable/compenents/el-data-table/utils/query'
2424
import { createSourceIdCache } from '@/api/common'
25+
import { download } from '@/utils/common'
2526
2627
export default {
2728
name: 'CommandList',
@@ -134,10 +135,7 @@ export default {
134135
queryUtil.stringify(query, '=', '&')
135136
url = url + queryStr
136137
this.$log.debug('Export url: ', this.url, '=>', url)
137-
const a = document.createElement('a')
138-
a.href = url
139-
a.click()
140-
window.URL.revokeObjectURL(url + queryStr)
138+
download(url + queryStr)
141139
}
142140
}
143141
},

0 commit comments

Comments
 (0)