AJAX



  • I want to send a picture of AJAX and get her data. I don't see it yet, but it's interesting and it's not working. And specifically my code:

    HTML

    <form id="myform">
        <input type="file" name="filename">
        <input type="button" name="button" value="Загрузить">
    </form>
    

    JS

    var form = document.forms.myform;
    form.elements.button.onclick = function(){
        var file = form.elements.filename.files[0];
        if(file){
            img();
        }else{
            alert("Выберете файл.");
        }
    }
    function img(){
        var formdata = new FormData(form.elements[0]);
        var xhr = new XMLHttpRequest();
        xhr.upload.onprogress = function(e){
            load.textContent = e.loaded + " / " + e.total;
        };
        xhr.open('POST', 'img.php');
        xhr.setRequestHeader('Content-Type', 'image/jpeg');
        xhr.send(formdata);
        xhr.onreadystatechange = function(){
            if(xhr.status != 200){
                alert("Status: " + xhr.status);
            }else{
                alert(xhr.responseText);
            }
        };
    }
    

    PHP

    <?php
    error_reporting(-1);
    print_r($_FILE['filename']['size']);
    ?>
    

    File didn't give up at all.The case was over. FormData() I should have told you. append , but he had to work so well. FormData(argument) ♪ It then issues:

    Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0 ------WebKitFormBoundaryWmXgtD5Kn1xyTV8q Content-Disposition: form-data; name="filename"; filename="5f944a3ea1b8fcb56bd54f326666a04b.jpg" Content-Type: image/jpeg ÿØÿàJFIFHHÿÛC...

    But why can't you get a name from the $FILES['filename']['name'].



  • For a sausage, you didn't have to put a global variable code.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2