S
S
semki0962016-03-27 19:47:09
JavaScript
semki096, 2016-03-27 19:47:09

The image is not loaded by Ajax - where is the error?

Uploading an image (Codeigniter)

<input class="myinput" name="userfile" type="file" />

$('.myinput').change(function(){
                    var fd = new FormData();     
                    $.ajax({
                        url: "/index.php/images/upload",
                        type: "POST",
                        data: fd,
                        processData: false,
                        contentType: false,
                        success: function(data){
                          alert(data);
                        }
                    });
                });


In the controller I write print_r( $_FILES )but returns an empty array. In the console I see that there are no errors. The image is also not loading anywhere. What's my mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dave, 2016-03-27
@semki096

The error is that you are passing empty data. Try wrapping it in a form:

<form>
  <input class="myinput" name="userfile" type="file" />
</form>

And then pass the serialized form:
var fd = new FormData($("form")[0]); // <- Передать данные с формы

E
Egor, 2016-03-28
@egormmm

CodeIgniter even has its own File Uploading Class library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question