点击按钮下载文件
- 已知文件的地址
- 图片地址必须与当前网页是同源
- download可以指定下载后的文件名
- 如果链接点击后,服务器的 HTTP 回应的头信息设置了
Content-Disposition
字段,并且该字段的值与download
属性不一致,那么该字段优先,下载时将显示其设置的文件名
<div>
<img src="/assets/1.png" alt="">
<a
href="./assets/1.png"
download="2.png"
>点击下载</a
>
</div>
<button @click="onClickLoad">点击下载</button>
<script>
const onClickLoad = () => {
fetch("./assets/1.png")
.then((res) => res.blob())
.then((res) => {
const url = URL.createObjectURL(res)
const aLink = document.createElement("a");
aLink.setAttribute("href",url)
aLink.download = Date.now()+".png"
aLink.click()
});
};
</script>
将html生成图片
- 通过使用
html2canvas
将一段html转化为canvas
<template>
<div class="box" ref="boxRef">
<img src="./assets/1.png" alt="" class="img">
</div>
<button @click="onCreateImgHandle">生成图片</button>
<div class="box1" ref="boxRef1"></div>
</template>
<script lang="ts" setup>
import { ref } from "vue"
import html2canvas from "html2canvas"
const boxRef = ref(null)
const boxRef1= ref(null)
const onCreateImgHandle = ()=>{
html2canvas(boxRef.value).then(res=>{
boxRef1.value.appendChild(res)
})
}
</script>
<style lang="less" scoped>
.box{
width: 300px;
height: 300px;
background: mediumaquamarine;
display: flex;
align-items: center;
justify-content: center;
.img{
width: 100px;
height: 100px;
display: block;
}
}
</style>
<template>
<div class="box" ref="boxRef">
<img src="./assets/1.png" alt="" class="img" />
</div>
<button @click="onCreateImgHandle">生成图片</button>
<div>
<img src="" alt="" ref="imgRef" />
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import html2canvas from "html2canvas";
const boxRef = ref(null);
const imgRef = ref(null);
const onCreateImgHandle = () => {
html2canvas(boxRef.value).then((res) => {
imgRef.value.src = res.toDataURL();
});
};
</script>
<style lang="less" scoped>
.box {
width: 300px;
height: 300px;
background: mediumaquamarine;
display: flex;
align-items: center;
justify-content: center;
.img {
width: 100px;
height: 100px;
display: block;
}
}
</style>
<template>
<div class="box" ref="boxRef">
<img src="./assets/1.png" alt="" class="img" />
<div class="box_text" ref="boxText" :style="{ fontSize: '16px' }">
{{
handleShowIssue(
"大家哦怕建瓯大家几哦啊加哦评价案件欧锦飒飒怕怕击破大将军庞大"
)
}}
</div>
</div>
<button @click="onCreateImgHandle">生成图片</button>
<div>
<img src="" alt="" ref="imgRef" />
</div>
</template>
<script lang="ts" setup>
import { ref, nextTick } from "vue";
import html2canvas from "html2canvas";
import { saveAs } from "file-saver";
const boxRef = ref(null);
const imgRef = ref(null);
const boxText = ref(null);
const handleShowIssue = (text) => {
if (boxText.value) {
const fontSize = parseFloat(boxText.value.style.fontSize);
const offsetWidth = boxText.value.offsetWidth;
const num = Math.floor(offsetWidth / fontSize);
return text.substring(0, num-1)+"...";
}
};
const onCreateImgHandle = () => {
html2canvas(boxRef.value).then((res) => {
imgRef.value.src = res.toDataURL();
res.toBlob((blob) => {
saveAs(blob, Date.now() + ".png");
});
});
};
</script>
<style lang="less" scoped>
.box {
width: 300px;
height: 300px;
background: mediumaquamarine;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
.img {
width: 100px;
height: 100px;
display: block;
}
.box_text {
overflow: hidden;
white-space: nowrap;
width: 100%;
}
}
</style>
html2canvas常见问题
html2canvas常见问题
file-saver使用方式
file-saver使用方式