0

I have written some watermark code in Laravel and it is working fine. But now I want to show the preview before submitting the file. But I am getting error "POST 419 (unknown status)"

My view source code is

<form class="form-horizontal" method="POST" action="{{ route('announcement.store') }}" enctype="multipart/form-data">
{{ csrf_field() }}

<label for="description">Description</label>
<textarea id="my-editor" class="textarea" name="description"  ></textarea>

<label for="image">Featured Image</label>
<input type="file" id="image" name="image">
<button type="submit" class="btn btn-success">Submit</button>
</form>

Javascript code

$(document).ready(function() {

$("#image").change(function(e) {
var image = $('#image').val();
     $.ajax({
       type: 'POST',
       url:'{{url('/my-admin/imageupload')}}',
       data: {image:image},
       success: function( msg ) {
           alert(msg);
       },
       error: function( data ) {
           alert(data);
       }
   });
   });

In this code, when I change the image I get error. I have done some watermark on image and save that image in folder. Now, I need to show that image on preview before submitting the form.

1
  • 1
    Add contentType: false, processData: false options for uploading file via ajax jQuery Ajax File Upload Commented Dec 29, 2017 at 13:41

3 Answers 3

1

Image will not be sent with this "var image = $('#image').val();" piece of code while using ajax request as you need to use data = new FormData(); for image.

Sign up to request clarification or add additional context in comments.

Comments

0

You missed _token as data to submit on ajax, so change the data to

        data: {
            "_token": "{{ csrf_token() }}",
            "image":image
        },

1 Comment

I still have save error "POST localhost:8000/my-admin/imageupload 419 (unknown status)"
0

I have successfully uploaded the image by this code.

$("#image").change(function(e) {
var data = new FormData();
data.append('image', $('input[type=file]')[0].files[0]);
data.append('_token', "{{ csrf_token() }}");
$.ajax({
        url:'{{url('/my-admin/imageupload')}}',
        type: 'POST',
        data : data,
        enctype : 'multipart/form-data',
        contentType: false,
        processData: false,
        success: function( data ) {
            var baseUrl = "{{asset('')}}";
            var imageUrl = baseUrl + data.msg;
            $('#changeimage').html('<img src="'+ imageUrl +'" height="120px" width="150px">');
        },
        error: function() {
            alert('unable to insert watermark on image');
        }
   });   
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.