vue中多附件上传的实现⽰例
⽬录
前⾔
核⼼代码
⽂件展⽰部分代码
前⾔
本篇主要记录在 Vue项⽬中实现附件上传功能,可实现单/多附件上传,识别⽂件格式并⽤不同图标展⽰功能,及控制是否可编辑功能。内容简洁易懂,如有需要可⾃取。
核⼼代码
<div class="upload-flie-btn">
<div class="btn-tips" @click="openFileSelect" v-show="editFlag">上传附件</div>
<input
type="file"
ref="fileInput"
multiple="multiple"
@change="uploadFile"
/>
</div>
openFileSelect() {
// dispatchEvent 向⼀个指定的事件⽬标派发⼀个事件
this.$refs.fileInput.dispatchEvent(new MouseEvent('click'));
}
/
/ 多附件上传
async uploadFile() {
let _this = this;
this.loading = true;
// 获取上传的⽂件,如要限制⽂件上传数量可以
// let files = [...this.$refs.fileInput.files].splice(0,limit);
// 也可以在此处抛出⽂件数量超出限制的提⽰
let files = [...this.$refs.fileInput.files];
if (!files || !files.length) {
return;
}
/
/ 后端接⼝地址
let url = `url`;
// ⼀起请求后端接⼝
Promise.all(
files.slice(0, files.length).map((file) => {
const data = new FormData();
data.append('file', file);
return request.post(url, data, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
})Ï
).then((res) => {
_this.loading = false;
_this.$refs.fileInput.value = null;
})
.catch((err) => {
console.log(err);
});
}
⽂件展⽰部分代码
此部分使⽤了 vux 组件库,该部分处理了word、excel、ppt、pdf、image、txt格式的⽂件展⽰问题,其它类型的⽂件均展⽰位“其它”,也可以⾃⾏更换⽮量图,iconfont 中可以查到。遍历的数据字段可根据⾃⼰的数据格式进⾏修改。
<swipeout v-if="files.length">
<template v-for="(item,index) in files">
<swipeout-item v-bind:key="item.fileUrl" :disabled="!editFlag">
<div slot="right-menu">
<swipeout-button @click.native="deleteItem(item.fileUrl, index)" type="warn">删除</swipeout-button>
</div>
<div slot="content" class="demo-content vux-1px-t">
<div class="file-item">
<a :href="item.url" rel="external nofollow" :download="item.fileName" class="file-look">
<!-- word -->
<div
v-if="item.fileName.indexOf('.doc') > 0 || item.fileName.indexOf('.docx') > 0"
class="file-icon"
>
<svg
t="1601351611486"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="/2000/svg"
p-id="1737"
width="200"
height="200"
>
<path
d="M1024 298.666667V85.333333c0-25.6-17.066667-42.666667-42.666667-42.666666H298.666667c-25.6 0-42.666667 17.066667-42.666667 42.666666v213.333334l384 85.333333 384-85.333333z"
fill="#41A5EE"
p-id="1738"
/>
<path
d="M1024 298.666667H256v213.333333l405.333333 85.333333 362.666667-85.333333z"
fill="#2B7CD3"
p-id="1739"
/>
<path d="M1024 512H256v213.333333l384 64 384-64z" fill="#185ABD" p-id="1740" />
<path
d="M1024 725.333333H256v213.333334c0 25.6 17.066667 42.666667 42.666667 42.666666h682.666666c25.6 0 42.666667-17.066667 42.666667-42.666666v-213.333334z"
fill="#103F91"
p-id="1741"
/>
<path
d="M588.8 256H256v597.333333h324.266667c29.866667 0 59.733333-29.866667 59.733333-59.733333V307.2c0-29.866667-21.333333-51.2-51.2-51.2z"
d="M546.133333 810.666667H51.2C21.333333 810.666667 0 789.333333 0 759.466667V264.533333C0 234.666667 21.333333 213.333333 51.2 213.333333h499.2c25.6 0 46.933333 21.333333 46.933333 51.2v499.2c0 25.6-21.33333
fill="#185ABD"
p-id="1743"
/>
<path
d="M435.2 682.666667H371.2L298.666667 448 226.133333 682.666667H162.133333L93.866667 341.333333h59.733333l46.933333 238.933334 72.533334-230.4h51.2l68.266666 230.4L443.733333 341.333333h59.733334l-68.266667 341.333334 fill="#FFFFFF"
p-id="1744"
/>
</svg>
</div>
<!-- excel -->
<div
v-else-if="item.fileName.indexOf('.xls') > 0 || item.fileName.indexOf('.xlsx') > 0"
class="file-icon"
>
<svg
t="1602124050240"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="/2000/svg"
p-id="1102"
width="200"
height="200"
>
<path
d="M682.666667 42.666667H298.666667c-25.6 0-42.666667 17.066667-42.666667 42.666666v213.333334l426.666667 213.333333 170.666666 64 170.666667-64V298.666667l-341.333333-256z"
fill="#21A366"
p-id="1103"
/>
<path
d="M256 298.666667h426.666667v213.333333H256z"
fill="#107C41"
p-id="1104"
/>
<path
d="M1024 85.333333v213.333334h-341.333333V42.666667h298.666666c21.333333 0 42.666667 21.333333 42.666667 42.666666z"
fill="#33C481"
p-id="1105"
/>
<path
d="M682.666667 512H256v426.666667c0 25.6 17.066667 42.666667 42.666667 42.666666h682.666666c25.6 0 42.666667-17.066667 42.666667-42.666666v-213.333334l-341.333333-213.333333z"
fill="#185C37"
p-id="1106"
/>
svg和h5的关系<path
d="M588.8 256H256v597.333333h324.266667c29.866667 0 59.733333-29.866667 59.733333-59.733333V307.2c0-29.866667-21.333333-51.2-51.2-51.2z"
opacity=".5"
p-id="1107"
/>
<path
d="M546.133333 810.666667H51.2C21.333333 810.666667 0 789.333333 0 759.466667V264.533333C0 234.666667 21.333333 213.333333 51.2 213.333333h499.2c25.6 0 46.933333 21.333333 46.933333 51.2v499.2c0 25.6-21.333333 46.93333 fill="#107C41"
p-id="1108"
/>
<path
d="M145.066667 682.666667L256 512 153.6 341.333333h81.066667l55.466666 106.666667c8.533333 12.8 8.533333 21.333333 12.8 25.6l12.8-25.6L375.466667 341.333333h76.8l-102.4 170.666667 106.666666 170.666667h-85.333333l-64-119.4 fill="#FFFFFF"
p-id="1109"
/>
<path
d="M682.666667 512h341.333333v213.333333h-341.333333z"
fill="#107C41"
p-id="1110"
/>
</svg>
</div>
<!-- ppt -->
<div
v-else-if="item.fileName.indexOf('.ppt') > 0 || item.fileName.indexOf('.pptx') > 0"
class="file-icon"
>
<svg
t="1602124175604"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="/2000/svg"
p-id="1258"
width="200"
height="200"
>
<path
d="M968.704 135.168h-430.08v752.64h430.08c15.36 0 26.624-12.288 26.624-26.624V162.816c0-15.36-11.264-27.648-26.624-27.648z"
fill="#FF8A65"
p-id="1259"
/>
<path
d="M512 646.144h376.832v53.248H512z m0 107.52h376.832v54.272H512z m161.792-483.328c-89.088 0-161.792 72.704-161.792 161.792C512 521.216 584.704 593.92 673.792 593.92s161.792-72.704 161.792-161.792H673.792V270.336z"
fill="#FBE9E7"
p-id="1260"
/>
<path
d="M727.04 216.064v161.792h161.792c0-89.088-72.704-161.792-161.792-161.792z"
fill="#FBE9E7"
p-id="1261"
/>
<path
d="M592.896 996.352L28.672 888.832V135.168L592.896 27.648z"
fill="#E64A19"
p-id="1262"
/>
<path
d="M319.488 327.68H192.512v368.64h78.848V569.344h40.96c44.032 0 78.848-11.264 104.448-34.816 25.6-22.528 38.912-53.248 38.912-90.112C455.68 366.592 409.6 327.68 319.488 327.68z m-14.336 178.176h-33.792V391.168h33.792c43.0 fill="#FFFFFF"
p-id="1263"
/>
</svg>
</div>
<!-- pdf -->
<div v-else-if="item.fileName.indexOf('.pdf') > 0" class="file-icon">
<svg
t="1602124241991"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="/2000/svg"
p-id="1536"
d="M877.874285 926.464244a48.742278 48.742278 0 0 1-48.793478 48.793478H146.432914a48.742278 48.742278 0 0 1-48.793478-48.793478V48.744838A48.742278 48.742278 0 0 1 146.432914 0.00256h418.814953a48.742278 48
fill="#EBECF0"
p-id="1537"
/>
<path
d="M877.874285 926.464244v48.793478a48.742278 48.742278 0 0 1-48.793478 48.742278H146.432914a48.742278 48.742278 0 0 1-48.793478-48.742278v-48.793478a48.742278 48.742278 0 0 0 48.793478 48.793478h682.647893a48.742278 4 fill="#C1C7D0"
p-id="1538"
/>
<path
d="M0.15488 536.372419H975.358842v243.813791a48.742278 48.742278 0 0 1-48.742279 48.742278H48.897158a48.742278 48.742278 0 0 1-48.742278-48.742278v-243.813791z"
fill="#FF5630"
p-id="1539"
/
>
<path
d="M97.639436 536.372419V438.836663L0.15488 536.372419h97.484556z m780.234849 0l0.972798-97.535756 97.023757 97.535756h-97.996555z"
fill="#DE350B"
p-id="1540"
/>
<path
d="M243.96867 585.165897h84.838188c15.513561-0.767998 30.668723 4.863988 41.932695 15.564761 10.803173 10.854373 16.639958 25.702336 16.07676 40.959898 0.511999 15.308762-5.324787 30.105525-16.07676 40.959897-11.673571 fill="#FFFFFF"
p-id="1541"
/
>
<path
d="M877.874285 312.577779v6.809583h-263.83294a48.742278 48.742278 0 0 1-48.742278-48.742279V0.00256a48.742278 48.742278 0 0 1 34.611113 14.131165l264.293739 263.83294c8.959978 9.215977 13.823965 21.708746 13.670366 34.6 fill="#C1C7D0"
p-id="1542"
/>
</svg>
</div>
<!-- image -->
<div
v-else-if="item.fileName.indexOf('.jpg') > 0 || item.fileName.indexOf('.jpeg') > 0 || item.fileName.indexOf
('.png') > 0"
class="file-icon"
>
<svg
t="1602124262555"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="/2000/svg"
p-id="1680"
width="200"
height="200"
>
<path
d="M901.565663 926.72a48.617739 48.617739 0 0 1-48.61774 48.662261H170.429663a48.617739 48.617739 0 0 1-48.61774-48.662261V48.662261A48.617739 48.617739 0 0 1 170.429663 0h418.860521a49.641739 49.641739 0 0 1 34.816 14. fill="#EBECF0"
p-id="1681"
/>
<path
d="M901.565663 926.72v48.617739a48.617739 48.617739 0 0 1-48.61774 48.617739H170.429663a48.617739 48.617739 0 0 1-48.61774-48.617739v-48.617739a48.617739 48.617739 0 0 0 48.61774 48.617739h682.51826a48.617739 48.617739 fill="#C1C7D0"
p-id="1682"
/>
<path
d="M24.042184 536.576h975.382261v243.712a48.617739 48.617739 0 0 1-48.662261 48.662261H72.659923A48.617739 48.617739 0 0 1 24.042184 780.288v-243.712z"
fill="#3EB7FC"
p-id="1683"
/>
<path
d="M121.856445 536.576V439.296L24.576445 536.576z m779.798261 0l1.024-97.28 97.28 97.28z"
fill="#2F9CCC"
p-id="1684"
/>
<path
d="M905.216445 312.32v6.633739h-264.192a48.617739 48.617739 0 0 1-48.662261-48.617739V0a49.641739 49.641739 0 0 1 34.816 14.336l263.479653 263.702261a48.484174 48.484174 0 0 1 14.336 34.326261z"
fill="#C1C7D0"
p-id="1685"
/>
<path
d="M354.259923 700.905739a87.930435 87.930435 0 0 1-17.808695 58.857739 62.775652 62.775652 0 0 1-50.710261 20.48 67.450435 67.450435 0 0 1-27.113739-5.12v-38.912a38.466783 38.466783 0 0 0 24.576 8.192c19.990261 0 29.696-14 fill="#FFFFFF"
p-id="1686"
/>
<path
d="M438.806706 709.097739v67.584h-41.494261v-193.536h66.56q72.214261 0 72.214261 61.44a57.388522 57.388522 0 0 1-22.038261 47.638261 80.940522 80.940522 0 0 1-54.761739 17.408h-20.48z m0-93.495652v62.330435h17.808696c24 fill="#FFFFFF"
p-id="1687"
/>
<path
d="M723.968445 763.859478a138.729739 138.729739 0 0 1-69.632 16.384 100.886261 100.886261 0 0 1-73.238261-26.089739 94.208 94.208 0 0 1-26.713043-71.234782 98.704696 98.704696 0 0 1 29.206261-74.21774 106.852174 106.852174 fill="#FFFFFF"
p-id="1688"
/>
</svg>
</div>
<!-- txt -->
<div v-else-if="item.fileName.indexOf('.txt') > 0" class="file-icon">
<svg
t="1602124341675"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="/2000/svg"
p-id="4641"
width="200"
height="200"
>
<path
d="M901.632 926.72c0 27.136-22.016 48.64-48.64 48.64H170.496c-27.136 0-48.64-22.016-48.64-48.64V48.64C121.856 22.016 143.36 0 170.496 0h418.816c12.8 0 25.6 5.12 34.816 14.336l263.68 263.68c9.216 9.216 14.336 21.504 14.336 34.8 fill="#EBECF0"
p-id="4642"
/>
<path
d="M901.632 926.72v48.64c0 27.136-22.016 48.64-48.64 48.64H170.496c-27.136 0-48.64-22.016-48.64-48.64V926.72c0 27.136 22.016 48.64 48.64 48.64h682.496c27.136 0 48.64-22.016 48.64-48.64z"
fill="#C1C7D0"
p-id="4643"
/>
<path
d="M24.064 536.576h975.36v243.712c0 27.136-22.016 48.64-48.64 48.64H72.704c-27.136 0-48.64-22.016-48.64-48.64v-243.712z"
fill="#0A84E8"
p-id="4644"
/>
<path
d="M121.856 536.576v-97.28l-97.28 97.28h97.28z m779.776 0l1.024-97.28 97.28 97.28h-98.304z"
fill="#005584"
p-id="4646"
/>
<path
d="M389.12 589.312v27.648H324.608v169.984H291.84v-169.984H227.328v-27.648H389.12zM466.432 589.312l46.592 69.632 46.592-69.632h39.424l-66.56 95.232 71.168 101.888h-39.424l-50.688-76.288-50.688 76.288h-39.424l70.656-101.888- fill="#FFFFFF"
p-id="4647"
/>
</svg>
</div>
<!-- other -->
<div v-else class="file-icon">
<svg
t="1602124370240"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="/2000/svg"
p-id="6067"
width="200"
height="200"
>
<path
d="M688.6 2H120.8c-17.1 0-27.9 12.9-27.9 35.7V1001c0 6.2 14 19.6 27.9 19.6h782.4c17.1 0 27.9-12.9 27.9-19.6V238.3c0-12.9-3.6-16-3.6-19.7L698.9 8.7c0-6.7-3.6-6.7-10.3-6.7z m0 0"
fill="#E7EFF8"
p-id="6068"
/>
<path
d="M93 1010.2c0 5.7 4.7 10.3 10.3 10.3h817.3c5.7 0 10.3-4.7 10.3-10.3V819.6H93v190.6z"
fill="#9FA0A3"
p-id="6069"
/>
<path
d="M340.8 915.4c0-10.1 3.5-18.5 10.5-25.3 7-6.8 15.8-10.1 26.2-10.1 10.4 0 19.2 3.3 26.5 10 7.2 6.7 10.8 15.1 10.8 25.4 0 10.6-3.6 19.2-10.7 25.8-7.2 6.6-16.1 9.8-26.8 9.8-10.3 0-18.9-3.4-26-10.1s-10.5-15.3-10.5-25.5z m128.5 0c0-10.2 3.5-18.7 fill="#FFFFFF"
p-id="6070"
/>
<path
d="M533.7 484.6h-66.8v-23.8c0-18 3.3-33.4 9.9-46.2 6.6-12.7 17.6-25.5 33.1-38.3 17.9-14.7 29.8-26.7 35.7-36.1 5.9-9.4 8.9-19.4 8.9-30.2 0-12.5-4.4-22.4-13.2-29.9-8.8-7.4-21.3-11.1-37.7-11.1-31.7 0-61.2 11.8-88.6 35.4v-77.9c30.2-16.9 62.5-25.3 fill="#9FA0A3"
p-id="6071"
/>
</svg>
</div>
<div class="file-name">{{item.fileName}}</div>
</a>
</div>
</div>
</swipeout-item>
</template>
</swipeout>
以上就是vue中多附件上传的实现⽰例的详细内容,更多关于vue 多附件上传的资料请关注其它相关⽂章!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论