Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
江名洋
/
linkwe-scrm
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 9a57e68f
authored
Jul 25, 2023
by
xinla
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
提取公用下载blob数据方法downloadBlob等
1 parent
fff4f329
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
21 deletions
vue3-lw-pc/src/utils/common.js
vue3-lw-pc/vite.config.js
vue3-lw-pc/src/utils/common.js
View file @
9a57e68
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
const
baseURL
=
window
.
lwConfig
.
BASE_API
const
baseURL
=
window
.
lwConfig
.
BASE_API
import
{
getToken
}
from
'@/utils/auth'
import
{
getToken
}
from
'@/utils/auth'
import
{
dateFormat
}
from
'@/utils/index'
import
axios
from
'axios'
import
axios
from
'axios'
// 日期格式化
// 日期格式化
...
@@ -95,6 +96,28 @@ export function selectDictLabels(datas, value, separator) {
...
@@ -95,6 +96,28 @@ export function selectDictLabels(datas, value, separator) {
return
actions
.
join
(
''
).
substring
(
0
,
actions
.
join
(
''
).
length
-
1
)
return
actions
.
join
(
''
).
substring
(
0
,
actions
.
join
(
''
).
length
-
1
)
}
}
/**
* 下载bolb文件
* @param {*} blob bolo源数据
* @param {*} type 文件类型
* @param {*} downloadName 下载文件名,需含文件后缀名
*/
export
function
downloadBlob
(
blob
,
type
,
downloadName
)
{
const
typeDict
=
{
excel
:
'application/vnd.ms-excel'
,
zip
:
'application/zip'
,
image
:
'application/image'
,
}
blob
=
new
Blob
([
blob
],
{
type
:
typeDict
[
type
]
||
type
})
let
url
=
window
.
URL
.
createObjectURL
(
blob
)
const
a
=
document
.
createElement
(
'a'
)
// 创建a标签
a
.
href
=
url
a
.
download
=
dateFormat
(
new
Date
(),
'YYYY-MM-DD HH:mm:ss-'
)
+
downloadName
// 下载文件名,不能包含英文 : 冒号
a
.
click
()
a
.
remove
()
URL
.
revokeObjectURL
(
url
)
// 释放内存
}
// 通用下载方法 系统内部
// 通用下载方法 系统内部
export
function
download
(
fileName
)
{
export
function
download
(
fileName
)
{
axios
({
axios
({
...
@@ -106,16 +129,18 @@ export function download(fileName) {
...
@@ -106,16 +129,18 @@ export function download(fileName) {
},
},
}).
then
((
res
)
=>
{
}).
then
((
res
)
=>
{
const
{
data
,
headers
}
=
res
const
{
data
,
headers
}
=
res
const
blob
=
new
Blob
([
data
],
{
type
:
headers
[
'content-type'
]
})
downloadBlob
(
data
,
headers
[
'content-type'
],
fileName
)
let
dom
=
document
.
createElement
(
'a'
)
let
url
=
window
.
URL
.
createObjectURL
(
blob
)
// const blob = new Blob([data], { type: headers['content-type'] })
dom
.
href
=
url
// let dom = document.createElement('a')
dom
.
download
=
fileName
// let url = window.URL.createObjectURL(blob)
dom
.
style
.
display
=
'none'
// dom.href = url
document
.
body
.
appendChild
(
dom
)
// dom.download = fileName
dom
.
click
()
// dom.style.display = 'none'
dom
.
parentNode
.
removeChild
(
dom
)
// document.body.appendChild(dom)
window
.
URL
.
revokeObjectURL
(
url
)
// dom.click()
// dom.parentNode.removeChild(dom)
// window.URL.revokeObjectURL(url)
})
})
}
}
...
@@ -130,16 +155,18 @@ export function downloadNet(url, fileName) {
...
@@ -130,16 +155,18 @@ export function downloadNet(url, fileName) {
},
},
}).
then
((
res
)
=>
{
}).
then
((
res
)
=>
{
const
{
data
,
headers
}
=
res
const
{
data
,
headers
}
=
res
const
blob
=
new
Blob
([
data
],
{
type
:
headers
[
'content-type'
]
})
downloadBlob
(
data
,
headers
[
'content-type'
],
fileName
)
let
dom
=
document
.
createElement
(
'a'
)
let
url
=
window
.
URL
.
createObjectURL
(
blob
)
// const blob = new Blob([data], { type: headers['content-type'] })
dom
.
href
=
url
// let dom = document.createElement('a')
dom
.
download
=
fileName
// let url = window.URL.createObjectURL(blob)
dom
.
style
.
display
=
'none'
// dom.href = url
document
.
body
.
appendChild
(
dom
)
// dom.download = fileName
dom
.
click
()
// dom.style.display = 'none'
dom
.
parentNode
.
removeChild
(
dom
)
// document.body.appendChild(dom)
window
.
URL
.
revokeObjectURL
(
url
)
// dom.click()
// dom.parentNode.removeChild(dom)
// window.URL.revokeObjectURL(url)
})
})
}
}
...
...
vue3-lw-pc/vite.config.js
View file @
9a57e68
...
@@ -53,7 +53,7 @@ export default defineConfig(async ({ command, mode }) => {
...
@@ -53,7 +53,7 @@ export default defineConfig(async ({ command, mode }) => {
},
},
// vite 相关配置
// vite 相关配置
server
:
{
server
:
{
port
:
1024
,
port
:
{
development
:
1024
,
test
:
1025
}[
mode
]
,
host
:
true
,
host
:
true
,
open
:
true
,
open
:
true
,
proxy
:
{
proxy
:
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment