3

I am implementing Jasny File Input plugin. However, I can't get it upload to server.

HTML

<form method="post" id="formCreateMod" class="form form-horizontal" enctype="multipart/form-data" role="form">
    <div class="fileinput fileinput-new" data-provides="fileinput">
         <div class="fileinput-preview thumbnail" data-trigger="fileinput"></div>
         <div>
              <span class="btn btn-default btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span><input type="file" name="img"></span>
              <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Delete</a>
         </div>
    </div>
</form>

The above snippet is inside the <form> tag. Then I use post in jQuery to send the serialized data of the form to server.

I am expecting to get the content in php by using $_FILES['img']["name"] or $_FILES['img']["type"], but the result is NULL.

So how should I retrieve the image data in php after the image is being posted?

Any help will be appreciated!

Update

The following is how I post the form in jQuery.

var theForm = $('form');
$.post(location.href, theForm.serialize(), function(data) {
    // handle return data
});
3
  • Can you post your <form> tag in your code as well ? (Yes, it is important) Commented Jan 27, 2014 at 12:26
  • @FlorianF. I don't quite get what you mean by post <form> tag. What I am currently posting is shown in the above under Update. Is that what you mean? Or is your way different? Commented Jan 27, 2014 at 20:51
  • @FlorianF. Sorry I didn't read it closely that you want me to post the <form> snippet. Not like post it to jquery. (Too much things) The <form> snippet is now updated above. Any clue? Commented Jan 29, 2014 at 0:52

1 Answer 1

7

You can't post a file using ajax and form serialization.

You should check this answer which explains why.

However, you still have solutions :

  • Not using ajax and just submitting the form using enctype='multipart/form-data' attribute on the form
<form [...] enctype='multipart/form-data'></form>
var formData = new FormData();

// HTML file input user's choice...
formData.append("userfile", fileInputElement.files[0]);

var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);
var reader = new FileReader();

reader.onload = function(e) {
  var rawData = reader.result;
}

reader.readAsBinaryString(file);
Sign up to request clarification or add additional context in comments.

5 Comments

Voting down without a comment ? Why do i bother trying to help :)
I didn't know file can't be uploaded with serialization. I'll try the methods you provided. And btw, my reputation won't let me vote down...
So with your FormData method above. How do I get the response from the server?
There's a pretty good example using jQuery ajax in this answer.
How do you get file element from Jasny file input?

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.