Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

HTML:

 <form id="upload" method="post" enctype="multipart/form-data">
      <input type="file" name="file" id="file"><br>
      <input type="submit" id="upload_button" name="upload_button" value="Upload">
 </form>

Javascript/jQuery:

$("#filefont").on("change", function(){
    var fileupload = document.getElementById("upload_font");

    formdata = new FormData(fileupload);

    var xmlreq = new XMLHttpRequest();
    xmlreq.open("POST", "uploadfont.php", true);
    xmlreq.onreadystatechange = function(event){
        if(xmlreq.readyState == 4 && xmlreq.status == 200){
            alert(xmlreq.responseText);
        }
    }
    xmlreq.send(formdata);
})

The javascript/jquery I have is something I just found off the internet. I tried lots of approaches but I can't seem to find a working way.

I already have a code in PHP, I just can't get the FILES from AJAX/JQuery.

share|improve this question
add comment

1 Answer

What you are doing is completly wrong ! A file must be sent to server multiparted. what you are doing is sending a file as plain/text !

Easily use jQuer File Upload. This is a good library and has a good documentation.

share|improve this answer
 
always Copy-paste is not a good idea.. –  StaticVariable 17 hours ago
 
Sorry. I tried a lot of things and didn't worked which led me here. I really quite don't understand what's happening with the code. –  user3029789 17 hours ago
 
@StaticVariable what do you mean ? –  Mohebifar 17 hours ago
 
@Mohebifar I think he meant I was copy pasting that's why I got a very wrong code. –  user3029789 17 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.