zoukankan      html  css  js  c++  java
  • vue通过input选取图片,jq的ajax向服务器上传img

    <template>
      <div class="">
        <!-- 选择后预览 -->
        <img v-if="imgOneUrl" :src="imgOneUrlalt="">
        <!-- 默认图片 -->
        <img v-else src="@/assets/images/upImg-1.png" alt="">
        <input type="file" name="file[]" class="file" accept="image/*" @change="upImgOne($event)">
      </div>
    </template>
    <script>
    export default {
        data(){
          return{
            imgOne:'',
            imgOneUrl:'',
          }
        },
        methods:{
            //选取并解读照片信息
            upImgOne(e) {
                this.imgOne = e.target.files[0];
                let reads = new FileReader();
                reads.readAsDataURL(this.imgOne);
                reads.onload = () => {
                    this.imgOneUrl = reads.result;
                    this.upLodeImg()        //多次上传文件多次调用这个方法,需要传参0,1,2
                }
            },
            upLodeImg(){
                var formData = new FormData() // 声明一个FormData对象
                var formData = new window.FormData() 
                formData.append('file'document.querySelector('input[type=file]').files[0])
                //formData.append('file', document.querySelectorAll('input[type=file]')[index].files[0])   //每次只上传单个文件,
                $.ajax({  
                    url: config.API + '/api/user/upload/xxx' ,  
                    type: 'POST',  
                    data: formData,  
                    async: false,  
                    cache: false,  
                    contentType: false,  
                    processData: false,  
                    success: function (resp) {  
                        console.log(resp);
                    },  
                    error: function (error) {  
                        alert(error);  
                    }  
                });
            },
        },
        mounted:function(){} 
    }
    </script>
    <style scoped>

    </style>
    upLodeImg(index){
                var formData = new FormData() // 声明一个FormData对象
                var formData = new window.FormData() 
                formData.append('file'document.querySelectorAll('input[type=file]')[index].files[0])
                formData.append('token'this.token)
                // let data = {
                //     token:this.token,
                //     file:formData,
                //     methodPost:true,
                // }
                let that = this
                let _that = this
                let _this = this
                let _thows = this
                $.ajax({  
                    url: config.API + '/api/user/upload/one' ,  
                    type: 'POST',  
                    data: formData,  
                    async: false,  
                    cache: false,  
                    contentType: false,  
                    processData: false,  
                    success: function (resp) {  
                        console.log(resp);
                        let data = resp.data
                        if(index==0){
                            _thows.firImg = data.url
                        }else if(index==1){
                            that.secImg = data.url
                        }else if(index==2){
                            _that.thrImg = data.url
                        }else{
                            _this.thiImg = data.url
                        }
                    },  
                    error: function (error) {  
                        alert(error);  
                    }  
                });
            },
  • 相关阅读:
    在做5道习题,个别习题有难度,需要很多天,加油把自己,有看到这个随笔的小伙伴评论可以留言讨论或发出你的答案~
    python中的open、close、read、write、len、exists
    python中的type
    python中strip、startswith、endswith
    python3_learn 实现文件夹内批量对图片重命名
    [转载]利用分块传输绕过WAF进行SQL注入
    bypass安全狗测试学习
    [转+自]SSH工作原理
    NCTF2019 小部分题解
    XML外部实体注入[转载]
  • 原文地址:https://www.cnblogs.com/wd163/p/13168979.html
Copyright © 2011-2022 走看看