简体   繁体   English

使用ajax上传laravel图片

[英]laravel image upload using ajax

I have one problem in my code.我的代码中有一个问题。 when I click on update button it does not upload an image in backend and in console message success is passed but image value it return null.当我单击更新按钮时,它不会在后端上传图像,并且在控制台消息中成功传递但图像值返回空值。 can anyone fix this solution.任何人都可以修复此解决方案。 I would be very grateful if you could this.如果你能做到这一点,我将不胜感激。 Here I have paste my blade code with script plz check this在这里,我用脚本粘贴了我的刀片代码,请检查这个

<div class="row">
    @foreach($client as $clie)
       <div class="col-4">
            <div class="form-group">
               <label for="clientimage">Logo #{{$loop->index+1}}</label>
               <input type="file" class="form-control-file" name="image" id="clientimage{{$clie->id}}">
               <small id="emailHelp" class="form-text text-muted">JPEG, PNG only</small>
            </div>
            <button type="submit" class="btn btn-primary float-right" onclick="updateImage({{$clie->id}})">update</button>
        </div>
    @endforeach
</div>

THis is my script code这是我的脚本代码

<script>
    function updateImage(id){
        var image = $('#clientimage' + id).val();
        // var subtext =  $('#subtext').val();
         $.ajax({
            headers: {
                 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
             },
            type: 'PUT',
            dataType: 'json',
            data: {image:image},
            url: `/cms/companyClient/${id}`,
            success: function( _response ){
                console.log(_response);
                console.log("file uploaded");
            },
            error: function( _response ){
                console.log(_response);
            }
         });
    }
</script>
try it my code 

<script type="text/javascript">

    $(document).ready(function (e) {

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('#image').change(function(){

            let reader = new FileReader();
            reader.onload = (e) => { 
              $('#image_preview_container').attr('src', e.target.result); 
            }
            reader.readAsDataURL(this.files[0]); 

        });

        $('#upload_image_form').submit(function(e) {
            e.preventDefault();

            var formData = new FormData(this);

            $.ajax({
                type:'POST',
                url: "{{ url('your ulr')}}",
                data: formData,
                cache:false,
                contentType: false,
                processData: false,
                success: (data) => {
                    this.reset();
                    alert('Image has been uploaded successfully');
                },
                error: function(data){
                    console.log(data);
                }
            });
        });
    });

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM