美文网首页
ajax上传

ajax上传

作者: 小柠有点萌 | 来源:发表于2020-04-14 13:07 被阅读0次

    ajax

    var formData = new FormData();
                formData.append("file", file); 
    $.ajax({  
                    url: "http://106.54.141.65:8088/fileUpload/uploadImg", 
                    type: 'POST',  
                    data: formData,  
                    dataType: 'JSON',  
                    cache: false,  
                    processData: false,  
                    contentType: false  
                  }).done(function (ret) {  
                    console.log(ret)
                    if(ret.success) {
                          console.log('上传成功');
                      } else {  
                          console.log('上传失败');
                      }  
                    });
    

    axios

                                        <input type="file" name="" id="" value="" @change="addImg($event)"  class="opacityImg"/>
    <script  src="https://lib.baomitu.com/axios/0.19.2/axios.min.js" type="text/javascript" charset="utf-8"></script>
    addImg(el,idx,dom){
          let _this=this;
          var file = el.target.files;
          var formData = new FormData();
          formData.append("file", file[0]); 
          var imgType = file[0];
            var type = imgType.type.split('/')[0];
              console.log(file[0])
              axios({
                method:'POST',
                url:'xxxx/fileUpload/uploadImg',
                headers: {
                'Content-Type':'multipart/form-data',
                },
                data: formData
          })
            .then(function (response) {
                console.log('上传成功');
            })
            .catch(function (error) {
              console.log('上传失败');
              layer.msg("重新上传")
            });
        },
    

    相关文章

      网友评论

          本文标题:ajax上传

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