Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
柳 佳乐
finance
Commits
f43d32ab
Commit
f43d32ab
authored
Nov 17, 2022
by
柳 佳乐
Browse files
20221117
parent
7c43290b
Changes
1
Show whitespace changes
Inline
Side-by-side
test.html
0 → 100644
View file @
f43d32ab
<body>
<button
onclick=
"download()"
>
下载(跨域)
</button>
<script>
function
download
()
{
var
url
=
'
http://39.174.37.19:11538/finance2/36f95c32cb9421b5df30aa99a99c788.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=1N6XFL78EG190UUFHM53%2F20221115%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221115T065613Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=0e3420279405fd4e88fbf33c997d12f13766dc7ffd9e4378fc5e43a229aa6236
'
;
var
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
'
GET
'
,
url
,
true
);
// 异步
xhr
.
responseType
=
'
blob
'
;
// blob 类型
xhr
.
onload
=
function
()
{
if
(
xhr
.
status
!=
200
)
{
alert
(
'
下载异常!
'
);
return
;
}
if
(
window
.
navigator
.
msSaveOrOpenBlob
)
{
navigator
.
msSaveBlob
(
xhr
.
response
,
filename
);
}
else
{
var
newUrl
=
window
.
URL
.
createObjectURL
(
xhr
.
response
);
var
a
=
document
.
createElement
(
'
a
'
);
a
.
setAttribute
(
'
href
'
,
newUrl
);
a
.
setAttribute
(
'
target
'
,
'
_blank
'
);
a
.
setAttribute
(
'
download
'
,
'
hh.jpg
'
);
// 自定义文件名(有效)
document
.
body
.
appendChild
(
a
);
a
.
click
();
document
.
body
.
removeChild
(
a
);
}
};
xhr
.
send
();
}
</script>
</body>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
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 comment