0

I am trying to create an image upload field in my application based on this question: Send FormData and String Data Together Through JQuery AJAX?
and this tutorial: http://www.formget.com/ajax-image-upload-php/
I have heard it is quite difficult, this is what i have tried.

HTML

<form method="POST" action="" id="logo_upload">
    <input type="file" name="logo_location" id="logo_location" enctype="multipart/form-data">
    <button type="submit" name="file_test" id="file_test">Test Upload</button>
</form>

JQuery

$('#logo_upload').submit(function(e) {
    e.preventDefault();
    var formData = new FormData();
    var file_data = $('#logo_location')[0].files[0];
    formData.append("file", file_data[0]);

    $.ajax({
        url: "../../../controllers/ajax_controller.php?action=image_upload",
        type: 'POST',
        data: formData ,
        cache: false,
        contentType: false,
        processData: false,
        id: id
    });
});

PHP

var_dump($_FILES);
var_dump($_POST);

As you can see, I haven't got to the uploading side of things yet.

Result

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=0)</i>
  <i><font color='#888a85'>empty</font></i>
</pre>

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=0)</i>
  <i><font color='#888a85'>empty</font></i>
</pre>

I can't see what i am doing wrong, I am getting a result so it is getting to the right place, can anyone point me in the right direction?

EDIT: added #logo_upload in form var file_data = $('#logo_location')[0].files[0];
EDIT: replaced data with formData variable
EDIT: added attribute: enctype="multipart/form-data"
New Result:

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=1)</i>
  'file' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'undefined'</font> <i>(length=9)</i>
</pre>
3
  • Point you in the right direction to what? Commented Apr 10, 2015 at 18:46
  • Silly question, but are you uploading the images, then manually browsing to http:// .. ../../../controllers/ajax_controller.php to view the dumps? Commented Apr 10, 2015 at 18:46
  • as in via a php request? no. the var_dump() is actually echoed back to the browser console Commented Apr 10, 2015 at 18:47

3 Answers 3

1

You're appending file_data[0] to the formdata object, file_data is the file not an array, use file_data.

$('#logo_upload').submit(function(e) {
    e.preventDefault();
    var formData = new FormData();
    var file_data = $('#logo_location')[0].files[0];
    formData.append("file", file_data);

    $.ajax({
        url: "../../../controllers/ajax_controller.php?action=image_upload",
        type: 'POST',
        data: formData ,
        contentType: false,
        processData: false,
        success: function(data){
            console.log(data);
        }
    });
});

also you can instantiate the form data object with the form in question instead of doing the append.

$('#logo_upload').submit(function(e) {
    e.preventDefault();
    var formData = new FormData(this);

    $.ajax({
    ...
Sign up to request clarification or add additional context in comments.

5 Comments

This was the way i thought it was supposed to be done but still nothing. I will double check now. How can i make sure that formData contains any data?
There is no way to, you can check your network tab in your developer tools to see what the browser sends though.
Ok that is extreamly useful, this is what chrome says was sent: ------WebKitFormBoundarysPx8WQQBwHAQAkPc Content-Disposition: form-data; name="file"; filename="IMG_0061.JPG" Content-Type: image/jpeg ------WebKitFormBoundarysPx8WQQBwHAQAkPc-- so i guess the image was sent? Now why is my script not receiving it :|
No it still dumps an empty array
This is the correct answer, after checking upload without ajax I found the image I was uploading was too large!
1
  1. I don't see logo_upload id in your form.
  2. When uploading a file enctype="multipart/form-data" is must in form attributes.
  3. data parameter in your ajax getting a variable i.e. not defined. Look at your reference link once again.

Hope this would help you

11 Comments

Hi, Thanks for your reply, i have tried this still now i am getting: <pre class='xdebug-var-dump' dir='ltr'> <b>array</b> <i>(size=1)</i> 'file' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'undefined'</font> <i>(length=9)</i> </pre>
#2 didn't help ? This one is the necessary part as well
Yes it helped i think because now i am getting 'undefined' rather than just 'empty'
I think you should try $("#logo_location").val() instead of $("#logo_location").get(0).files
I tried this, I now get 'C' instead of 'undefined'... this is from var_dump($_POST) by the way.
|
0

You are passing wrong variable here and you are not defining success in your ajax request:

$('#logo_upload').submit(function(e) {
e.preventDefault();
var formData = new FormData($('#your_form_id')[0]);

$.ajax({
    url: "../../../controllers/ajax_controller.php?action=image_upload",
    type: 'POST',
    data: formData,
    success: function(result){
        alert(result);
    }
    cache: false,
    contentType: false,
    processData: false
});
});

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.