欢迎来到山村网

vue中用H5实现文件上传的方法实例

2019-03-09 12:30:24浏览:905 来源:山村网   
核心摘要:整理文档,搜刮出一个vue中用H5实现文件上传的方法实例代码,稍微整理精简一下做下分享。1.图片上传 img v-if=personInfo.photoU

整理文档,搜刮出一个vue中用H5实现文件上传的方法实例代码,稍微整理精简一下做下分享。

1.图片上传

 <img v-if="personInfo.photoUrl" :src=http://www.shancun.net/skin/default/image/nopic.gif + personInfo.photoUrl" style="height:126px;max-width:133px;margin: 25px 0;"> <img v-else src=http://www.shancun.net/skin/default/image/nopic.gif 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  }}
(责任编辑:豆豆)
下一篇:

Centos硬件信息查看命令

上一篇:

局域网打印机共享怎么设置?

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com