美文网首页
js图片上传

js图片上传

作者: 非_MO | 来源:发表于2019-07-16 15:02 被阅读0次
写个图片上传的功能,没有检验图片大小,只是把图片转了base64 格式,然后再由后台进行操作才使用,也可以直接使用,只是没有转base64,相关代码如下
<input type="file" @change="imgChange" accept="image/*">

在js中

imgChange(files) {
    let file = files.target.files[0]
    let reader = new FileReader();
    let imgFile;
    reader.readAsDataURL(file)
    reader.onload = e => {
        imgFile = e.target.result;
        let arr = imgFile.split(',')
     
        let url = '/impwx/personal/modify'
        this.$store.state.loginForm.personImg = arr[1]
        this.$http.modity(url, this.$store.state.loginForm).then(res => {
            console.log(res)
            if (res.code == 200) {
                this.$store.commit('setLoginForm', res.data)
                this.$router.push({
                    path: '/wxScreen/personal'
                })
            } else {
                this.$toast.loading({
                    message: res.message
                });
            }
        })
    }
},

因为主要是想记载下图片在上传过程中的转base64方法,因此,这个功能只是基础,也没有进行图片尺寸的校验,如果有需要,后续可能会进行优化更新。

相关文章

网友评论

      本文标题:js图片上传

      本文链接:https://www.haomeiwen.com/subject/fzctlctx.html