fix 下载文件
Showing
4 changed files
with
62 additions
and
31 deletions
| ... | @@ -38,12 +38,25 @@ export const getDownFileSignByUrl = (fileName) => { | ... | @@ -38,12 +38,25 @@ export const getDownFileSignByUrl = (fileName) => { |
| 38 | }); | 38 | }); |
| 39 | }; | 39 | }; |
| 40 | 40 | ||
| 41 | export const getImageContent = (params) => request({ | 41 | //obs下载 |
| 42 | url: `${import.meta.env.VITE_APP_COMMON_URL}/obs/download-file-stream?filePath=${params.signedUrl}`, | 42 | export const obsDownloadRequest = (params) => { |
| 43 | method: 'get', | 43 | return request({ |
| 44 | responseType: 'blob', | 44 | withCredentials: false, |
| 45 | headers: { 'Content-Type': params.actualSignedRequestHeaders['Content-Type'] } | 45 | headers: params.actualSignedRequestHeaders |
| 46 | }); | 46 | ? { |
| 47 | "Content-Type": params.actualSignedRequestHeaders["Content-Type"], | ||
| 48 | } | ||
| 49 | : {}, | ||
| 50 | validateStatus: function (status) { | ||
| 51 | return status >= 200; | ||
| 52 | }, | ||
| 53 | url: params.signedUrl, | ||
| 54 | responseType: "blob", | ||
| 55 | maxRedirects: 0, | ||
| 56 | data: { unused: 0 }, | ||
| 57 | method: "get", | ||
| 58 | }); | ||
| 59 | }; | ||
| 47 | 60 | ||
| 48 | //获取上传签名 | 61 | //获取上传签名 |
| 49 | export const getUpFileSignByUrl = (params) => { | 62 | export const getUpFileSignByUrl = (params) => { | ... | ... |
| ... | @@ -24,9 +24,11 @@ import useUserStore from "@/store/modules/user"; | ... | @@ -24,9 +24,11 @@ import useUserStore from "@/store/modules/user"; |
| 24 | // getImageContent | 24 | // getImageContent |
| 25 | // } from '@/api/modules/queryService'; | 25 | // } from '@/api/modules/queryService'; |
| 26 | import { | 26 | import { |
| 27 | getImageContent, | 27 | parseAndDecodeUrl, |
| 28 | getUpFileSignByUrl, | 28 | getUpFileSignByUrl, |
| 29 | obsUploadRequest | 29 | obsUploadRequest, |
| 30 | getDownFileSignByUrl, | ||
| 31 | obsDownloadRequest | ||
| 30 | } from "@/api/modules/obsService"; | 32 | } from "@/api/modules/obsService"; |
| 31 | import { Editor, EditorExpose } from '@/components/Editor' | 33 | import { Editor, EditorExpose } from '@/components/Editor' |
| 32 | 34 | ||
| ... | @@ -332,10 +334,15 @@ const handlePictureCardPreview = (file, item) => { | ... | @@ -332,10 +334,15 @@ const handlePictureCardPreview = (file, item) => { |
| 332 | uploadPreviewVisible.value = true; | 334 | uploadPreviewVisible.value = true; |
| 333 | } | 335 | } |
| 334 | 336 | ||
| 335 | const onUploadFilePreview = (file, item) => { | 337 | const onUploadFilePreview = async (file, item) => { |
| 336 | let f = formInline.value[item.field].find(i => i.name == file.name); | 338 | let f = formInline.value[item.field].find(i => i.name == file.name); |
| 337 | let url = f.url; | 339 | let url = f.url; |
| 338 | getImageContent(url).then((res: any) => { | 340 | const refSignInfo: any = await getDownFileSignByUrl(parseAndDecodeUrl(url).decodedPath); |
| 341 | if (!refSignInfo?.data) { | ||
| 342 | refSignInfo?.msg && ElMessage.error(refSignInfo?.msg); | ||
| 343 | return; | ||
| 344 | } | ||
| 345 | obsDownloadRequest(refSignInfo?.data).then((res: any) => { | ||
| 339 | if (res && !res.msg) { | 346 | if (res && !res.msg) { |
| 340 | let name = f.name; | 347 | let name = f.name; |
| 341 | var fileSuffix = name ? name.substring(name.lastIndexOf('.') + 1).toLowerCase() : ''; | 348 | var fileSuffix = name ? name.substring(name.lastIndexOf('.') + 1).toLowerCase() : ''; |
| ... | @@ -346,20 +353,19 @@ const onUploadFilePreview = (file, item) => { | ... | @@ -346,20 +353,19 @@ const onUploadFilePreview = (file, item) => { |
| 346 | } else { | 353 | } else { |
| 347 | download(res, name, fileSuffix); | 354 | download(res, name, fileSuffix); |
| 348 | } | 355 | } |
| 349 | // if (fileSuffix === 'png' || fileSuffix === 'jpeg' || fileSuffix === 'jpg' || fileSuffix === 'pdf' || fileSuffix === 'zip' || fileSuffix === 'rar') { | ||
| 350 | // let win = window.open(fileUrl, name); | ||
| 351 | // win && (win.document.title = name); | ||
| 352 | // return win; | ||
| 353 | // } | ||
| 354 | // window.open('https://view.officeapps.live.com/op/view.aspx?src=' + encodeURIComponent(fileUrl)); | ||
| 355 | } else { | 356 | } else { |
| 356 | res?.msg && ElMessage.error(res?.msg); | 357 | res?.msg && ElMessage.error(res?.msg); |
| 357 | } | 358 | } |
| 358 | }) | 359 | }); |
| 359 | } | 360 | } |
| 360 | 361 | ||
| 361 | const downloadTemplate = (url) => { | 362 | const downloadTemplate = async (url) => { |
| 362 | getImageContent(url).then((res: any) => { | 363 | const refSignInfo: any = await getDownFileSignByUrl(parseAndDecodeUrl(url).decodedPath); |
| 364 | if (!refSignInfo?.data) { | ||
| 365 | refSignInfo?.msg && ElMessage.error(refSignInfo?.msg); | ||
| 366 | return; | ||
| 367 | } | ||
| 368 | obsDownloadRequest(refSignInfo?.data).then((res: any) => { | ||
| 363 | if (res && !res.msg) { | 369 | if (res && !res.msg) { |
| 364 | let urlNames = url.split('/'); | 370 | let urlNames = url.split('/'); |
| 365 | let name = urlNames.at(-1); | 371 | let name = urlNames.at(-1); |
| ... | @@ -368,12 +374,17 @@ const downloadTemplate = (url) => { | ... | @@ -368,12 +374,17 @@ const downloadTemplate = (url) => { |
| 368 | } else { | 374 | } else { |
| 369 | res?.msg && ElMessage.error(res?.msg); | 375 | res?.msg && ElMessage.error(res?.msg); |
| 370 | } | 376 | } |
| 371 | }) | 377 | }); |
| 372 | } | 378 | } |
| 373 | 379 | ||
| 374 | const onUploadFileDownload = (file, item) => { | 380 | const onUploadFileDownload = async (file, item) => { |
| 375 | let url = file.url; | 381 | let url = file.url; |
| 376 | getImageContent(url).then((res: any) => { | 382 | const refSignInfo: any = await getDownFileSignByUrl(parseAndDecodeUrl(url).decodedPath); |
| 383 | if (!refSignInfo?.data) { | ||
| 384 | refSignInfo?.msg && ElMessage.error(refSignInfo?.msg); | ||
| 385 | return; | ||
| 386 | } | ||
| 387 | obsDownloadRequest(refSignInfo?.data).then((res: any) => { | ||
| 377 | if (res && !res.msg) { | 388 | if (res && !res.msg) { |
| 378 | let f = formInline.value[item.field].find(i => i.name == file.name); | 389 | let f = formInline.value[item.field].find(i => i.name == file.name); |
| 379 | let name = f.name; | 390 | let name = f.name; | ... | ... |
| ... | @@ -298,6 +298,16 @@ export const downFile = (fileUrl, fileName) => { | ... | @@ -298,6 +298,16 @@ export const downFile = (fileUrl, fileName) => { |
| 298 | }, 66) | 298 | }, 66) |
| 299 | } | 299 | } |
| 300 | 300 | ||
| 301 | //本地文件下载根据Bob流 | ||
| 302 | export const downFileByBob = (data,fileName) => { | ||
| 303 | let blob = new Blob([data],{type:data.type || 'text'}); // #识别文件类型 | ||
| 304 | let objectUrl = URL.createObjectURL(blob); | ||
| 305 | const link = document.createElement('a'); | ||
| 306 | link.download = decodeURIComponent(fileName); | ||
| 307 | link.href = objectUrl; | ||
| 308 | link.click(); | ||
| 309 | } | ||
| 310 | |||
| 301 | // 表单提交参数 | 311 | // 表单提交参数 |
| 302 | export const setFormFields = (list) => { | 312 | export const setFormFields = (list) => { |
| 303 | let obj = {}; | 313 | let obj = {}; | ... | ... |
| ... | @@ -11,7 +11,7 @@ import Tabs from '@/components/Tabs/index.vue' | ... | @@ -11,7 +11,7 @@ import Tabs from '@/components/Tabs/index.vue' |
| 11 | import Table from '@/components/Table/index.vue' | 11 | import Table from '@/components/Table/index.vue' |
| 12 | import Dialog from '@/components/Dialog/index.vue' | 12 | import Dialog from '@/components/Dialog/index.vue' |
| 13 | import useCatchStore from "@/store/modules/catch"; | 13 | import useCatchStore from "@/store/modules/catch"; |
| 14 | import { download, downFile, getDownloadUrl } from '@/utils/common' | 14 | import { download, downFileByBob, downFile } from '@/utils/common' |
| 15 | import { | 15 | import { |
| 16 | addImportData, | 16 | addImportData, |
| 17 | deleteImportData, | 17 | deleteImportData, |
| ... | @@ -23,7 +23,7 @@ import { | ... | @@ -23,7 +23,7 @@ import { |
| 23 | import { | 23 | import { |
| 24 | parseAndDecodeUrl, | 24 | parseAndDecodeUrl, |
| 25 | getDownFileSignByUrl, | 25 | getDownFileSignByUrl, |
| 26 | getImageContent | 26 | obsDownloadRequest |
| 27 | } from '@/api/modules/obsService'; | 27 | } from '@/api/modules/obsService'; |
| 28 | import { commonPageConfig } from '@/utils/enum'; | 28 | import { commonPageConfig } from '@/utils/enum'; |
| 29 | 29 | ||
| ... | @@ -226,11 +226,9 @@ const tableBtnClick = async (scope, btn) => { | ... | @@ -226,11 +226,9 @@ const tableBtnClick = async (scope, btn) => { |
| 226 | refSignInfo?.msg && ElMessage.error(refSignInfo?.msg); | 226 | refSignInfo?.msg && ElMessage.error(refSignInfo?.msg); |
| 227 | return; | 227 | return; |
| 228 | } | 228 | } |
| 229 | getImageContent(refSignInfo?.data).then((res: any) => { | 229 | obsDownloadRequest(refSignInfo?.data).then((res: any) => { |
| 230 | if (res && !res.msg) { | 230 | if (res && !res.msg) { |
| 231 | let name = row.filePath; | 231 | downFileByBob(res, row.fileName); |
| 232 | var fileSuffix = name ? name.substring(name.lastIndexOf('.') + 1) : ''; | ||
| 233 | download(res, row.fileName, fileSuffix); | ||
| 234 | } else { | 232 | } else { |
| 235 | res?.msg && ElMessage.error(res?.msg); | 233 | res?.msg && ElMessage.error(res?.msg); |
| 236 | } | 234 | } |
| ... | @@ -243,12 +241,11 @@ const tableBtnClick = async (scope, btn) => { | ... | @@ -243,12 +241,11 @@ const tableBtnClick = async (scope, btn) => { |
| 243 | refSignInfo?.msg && ElMessage.error(refSignInfo?.msg); | 241 | refSignInfo?.msg && ElMessage.error(refSignInfo?.msg); |
| 244 | return; | 242 | return; |
| 245 | } | 243 | } |
| 246 | getImageContent(refSignInfo?.data).then((res: any) => { | 244 | obsDownloadRequest(refSignInfo?.data).then((res: any) => { |
| 247 | if (res && !res.msg) { | 245 | if (res && !res.msg) { |
| 248 | let name = row.errorFilePath; | 246 | let name = row.errorFilePath; |
| 249 | var fileSuffix = name ? name.substring(name.lastIndexOf('.') + 1) : ''; | ||
| 250 | let fileName = name ? name.substring(name.lastIndexOf('/') + 1) : '' | 247 | let fileName = name ? name.substring(name.lastIndexOf('/') + 1) : '' |
| 251 | download(res, fileName, fileSuffix); | 248 | downFileByBob(res, fileName); |
| 252 | } else { | 249 | } else { |
| 253 | res?.msg && ElMessage.error(res?.msg); | 250 | res?.msg && ElMessage.error(res?.msg); |
| 254 | } | 251 | } | ... | ... |
-
Please register or sign in to post a comment