【更新】针对前端下载blob提出重复代码

pull/89/MERGE
小诺 2023-03-11 22:34:22 +08:00 committed by 俞宝山
parent d552d86dd2
commit 87f43b5f17
3 changed files with 6 additions and 30 deletions

View File

@ -60,6 +60,7 @@
<script setup name="genIndex"> <script setup name="genIndex">
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import downloadUtil from '@/utils/downloadUtil'
import steps from './steps.vue' import steps from './steps.vue'
import genPreview from './preview.vue' import genPreview from './preview.vue'
import genBasicApi from '@/api/gen/genBasicApi' import genBasicApi from '@/api/gen/genBasicApi'
@ -173,16 +174,7 @@
} else { } else {
// //
genBasicApi.basicExecGenBiz(param).then((res) => { genBasicApi.basicExecGenBiz(param).then((res) => {
const blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' }) downloadUtil.resultDownload(res)
const contentDisposition = res.headers['content-disposition']
const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
const $link = document.createElement('a')
$link.href = URL.createObjectURL(blob)
$link.download = decodeURIComponent(patt.exec(contentDisposition)[1])
$link.click()
document.body.appendChild($link)
document.body.removeChild($link) //
window.URL.revokeObjectURL($link.href) // blob
}) })
} }
} }

View File

@ -49,6 +49,7 @@
</template> </template>
<script setup name="genSteps"> <script setup name="genSteps">
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import downloadUtil from '@/utils/downloadUtil'
import basic from './basic.vue' import basic from './basic.vue'
import config from './config.vue' import config from './config.vue'
import genPreview from './preview.vue' import genPreview from './preview.vue'
@ -133,16 +134,7 @@
} else { } else {
// //
genBasicApi.basicExecGenBiz(param).then((res) => { genBasicApi.basicExecGenBiz(param).then((res) => {
const blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' }) downloadUtil.resultDownload(res)
const contentDisposition = res.headers['content-disposition']
const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
const $link = document.createElement('a')
$link.href = URL.createObjectURL(blob)
$link.download = decodeURIComponent(patt.exec(contentDisposition)[1])
$link.click()
document.body.appendChild($link)
document.body.removeChild($link) //
window.URL.revokeObjectURL($link.href) // blob
emit('closed') emit('closed')
}) })
} }

View File

@ -38,6 +38,7 @@
<script setup name="userImpExp"> <script setup name="userImpExp">
import userApi from '@/api/sys/userApi' import userApi from '@/api/sys/userApi'
import downloadUtil from "@/utils/downloadUtil"
const impUploadLoading = ref(false) const impUploadLoading = ref(false)
const impAlertStatus = ref(false) const impAlertStatus = ref(false)
@ -93,16 +94,7 @@
// //
const downloadImportUserTemplate = () => { const downloadImportUserTemplate = () => {
userApi.userDownloadImportUserTemplate().then((res) => { userApi.userDownloadImportUserTemplate().then((res) => {
const blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' }) downloadUtil.resultDownload(res)
const contentDisposition = res.headers['content-disposition']
const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
const $link = document.createElement('a')
$link.href = URL.createObjectURL(blob)
$link.download = decodeURIComponent(patt.exec(contentDisposition)[1])
$link.click()
document.body.appendChild($link)
document.body.removeChild($link) //
window.URL.revokeObjectURL($link.href) // blob
}) })
} }
// //