2014-12-24 7 views
8

pracuję nad plików uploader która przesyła obraz, gdy wejście zostanie zmienione mojego kodu do formularza w html jestWarning: Missing granicę w danych POST wieloczęściowy/form-data w nieznane na linii 0

<form method="post" enctype="multipart/form-data"> 
    <input name="uploaded[]" type="file" id="file_upload"/> 
</form> 

Moja JavaScript i Ajax:

 document.getElementById("file_upload").onchange = function() { 
      var id = document.getElementById("user_id").innerHTML; 
      var file = document.getElementById("file_upload").files[0]; 
      alert(file.size); 
      var formdata = new FormData(); 
      formdata.append("filer",file,true); 
      var ajax = new XMLHttpRequest(); 
      ajax.onreadystatechange = 
      function(){ 
       if(ajax.readyState==4 && ajax.status==200){ 
        document.getElementById("one").remove(); 
        var img = document.createElement('img'); 
        var first_path = '/user_image/'; 
        var path = first_path.concat(id,'.png'); 
        img.setAttribute('alt','User image'); 
        img.setAttribute('id','one');       
        img.setAttribute('src',path); 
        document.getElementById("user").appendChild(img); 
        alert("end"); 
       }  
       else{ 
        document.getElementById("one").remove(); 
        var img = document.createElement('img'); 
        img.setAttribute('src','/img/loading.gif'); 
        img.setAttribute('alt','User image'); 
        img.setAttribute('id','one'); 
        document.getElementById("user").appendChild(img);       
       } 
      }    
      ajax.open("POST","upload_image.php"); 
      ajax.setRequestHeader("Content-Type", "multipart/form-data"); 
      ajax.send(formdata); 
     }; 

a mój kod php jest proste, jest po prostu sprawdzić, czy wszystko jest ok

require("../includes/config.php"); //config folder to start the session 
if($_SERVER["REQUEST_METHOD"]=="POST"){ 
     echo '<pre>',print_r($_FILES),'</pre>'; //dumping some variable and arrays to see where the problem is 
} 

Żądanie, które otrzymuję z serwera, to Ostrzeżenie: Brakujące dane w wieloczęściowym/formularzy Dane POST w Nieznane w wierszu 0, ale wysłałem formularze i nagłówek żądania i otworzyłem plik.

Odpowiedz

12

Trzeba tylko usunąć następujący wiersz:

ajax.setRequestHeader("Content-Type", "multipart/form-data");

0

Zamiast wysyłać dane jako multipart/form-data:

ajax.setRequestHeader("Content-Type", "multipart/form-data"); 

Należy wysłać go jako application/json:

ajax.setRequestHeader("Content-Type", "application/json"); 
+0

Jeśli musisz przesłać nazwę pliku utf-8? – gtzinos

Powiązane problemy