0

I am using ajax to upload a photo in Laravel. URL is working fine but data is not receiving in function in a controller.

HTML Code

<input type="file" id="photo_input">

Ajax / JQuery Code

$('#photo_input').change(function(){
var formData = new FormData('#photo_input');    
$.ajax({
       type:'POST',
       url: home_url+'/upload-image',
       data:formData,
       cache:false,
       enctype: 'multipart/form-data',
       contentType: false,
       processData: false,
       success:function(data){
               console.log("success");
                console.log(data);
            },
            error: function(data){
                console.log("error");
                console.log(data);
            }
        });
    });


PHP Code in Controller

public function uploadImage(){
  $data = Input::all();
  print_r($data);
}


Response from function in a controller

array(
);

I haven't used <form> tag in html, I want to upload it directly. Do I need to add <form> tag? Is there anything which is missing?

4
  • @SagarGautam I am not using the form for this, do I need to add <form> tag for this? Commented Aug 15, 2017 at 10:20
  • Yes, create a form for the input and on submit do ajax request: Commented Aug 15, 2017 at 10:23
  • @Omkar see this question. might help you stackoverflow.com/questions/166221/… Commented Aug 15, 2017 at 10:25
  • And yes you can do it without form as well! Commented Aug 15, 2017 at 10:28

2 Answers 2

1
$("#example-form").submit(function(){

var formData = new FormData($(this)[0]);
    $.ajax({
        data: formData
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

@milo Thanks for answering but I am not using <form> so can you please suggest how do I upload file without using <form> tag (if possible)?
You can use base64, post base64 data with ajax; stackoverflow.com/a/36281449/2693988 Later use PHP to image method; stackoverflow.com/a/15153931/2693988
0

Do it like this:

<form method="POST" id="photoform" enctype="multipart/form-data">
<input type="file" id="photo_input">
</form>

And accordingly ajax request should change like this:

$('#photoform').submit(function(){
var fileupload = $('#photo_input').val();    
$.ajax({
       type:'POST',
       url: home_url+'/upload-image',
       data:{
           file: fileupload,
       },
       cache:false,
       enctype: 'multipart/form-data',
       contentType: false,
       processData: false,
       success:function(data){
               console.log("success");
                console.log(data);
            },
            error: function(data){
                console.log("error");
                console.log(data);
            }
        });
    });

3 Comments

You can also do it without form as discussed in this url stackoverflow.com/questions/166221/…
Your code wont work because this is not a way to upload a file using ajax. You will have to use formdata to upload files.
@Farsay it will definitely work. I have used it in such way.

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.