vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼
更新時(shí)間:2017年05月27日 11:50:02 作者:于曉宇
本篇文章主要介紹了vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
整理文檔,搜刮出一個(gè)vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼,稍微整理精簡(jiǎn)一下做下分享。
1.圖片上傳
<img v-if="personInfo.photoUrl" :src="headPreFix + personInfo.photoUrl" style="height:126px;max-width:133px;margin: 25px 0;"> <img v-else src="../../assets/default.png" style="height:126px;max-width:133px;margin: 25px 0;">
<form id="form1" enctype="multipart/form-data" method="post" action=""> <div style="height:0px; overflow:hidden; position:absolute;"> <input type="file" tabIndex="-1" accept="image/jpeg,image/x-png,image/gif" name="file" style="padding-left:10px" id="fileToUpload" @change="fileSelected()"/> </div> <button type="button" class="btn btn-default btn-xs" onclick="document.getElementById('fileToUpload').click()">上傳</button> <button type="button" class="btn btn-default btn-xs" @click="deleteImg">刪除</button> </form>
// 上傳圖片 'fileSelected': function () { var that = this let files = document.getElementById('fileToUpload').files if (files && files.length) { var fd = new FormData() fd.append('file', files[0]) var reader = new window.FileReader() // 圖片大小 100KB var fileSize = 100 * 1024 reader.readAsDataURL(files[0]) reader.onload = function (e) { if (e.target.result.length > fileSize) { that.$dispatch('show-alert', 'msg_1016') document.getElementById('fileToUpload').value = '' } else { var xhr = new XMLHttpRequest() xhr.addEventListener('load', that.uploadComplete, false) xhr.open('POST', that.$root.appBaseUrl + 'fileUpload/singleFileUpload') xhr.send(fd) } } } }, // 上傳成功 'uploadComplete': function (evt) { this.personInfo.photoUrl = (evt.target.responseText).replace('\\', '/') document.getElementById('fileToUpload').value = '' }, // 刪除圖片 'deleteImg': function () { this.personInfo.photoUrl = '' },
computed: { headPreFix: function () { let params = window.localdicts.dicts.ph_params.systemParam if (params.storageType === 1) { return params.storageUrl } return this.$root.appBaseUrl } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vue實(shí)現(xiàn)文件上傳功能
- vue中使用input[type="file"]實(shí)現(xiàn)文件上傳功能
- SpringBoot+Vue.js實(shí)現(xiàn)前后端分離的文件上傳功能
- vue使用axios實(shí)現(xiàn)文件上傳進(jìn)度的實(shí)時(shí)更新詳解
- vue-cli+axios實(shí)現(xiàn)文件上傳下載功能(下載接收后臺(tái)返回文件流)
- vue webuploader 文件上傳組件開發(fā)
- vue+vux實(shí)現(xiàn)移動(dòng)端文件上傳樣式
- vue實(shí)現(xiàn)文件上傳讀取及下載功能
- vue中實(shí)現(xiàn)圖片和文件上傳的示例代碼
- vue+axios+java實(shí)現(xiàn)文件上傳功能
相關(guān)文章
vue3+ts+vite+electron搭建桌面應(yīng)用的過程
這篇文章主要介紹了vue3+ts+vite+electron搭建桌面應(yīng)用的過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04laravel5.3 vue 實(shí)現(xiàn)收藏夾功能實(shí)例詳解
這篇文章主要介紹了laravel5.3 vue 實(shí)現(xiàn)收藏夾功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01vue3不能使用history.pushState修改url參數(shù)踩坑
這篇文章主要為大家介紹了vue3不能使用history.pushState修改url參數(shù)踩坑解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02