2013-05-02 11 views
5

Próbuję użyć FormData do wysyłania danych przez AJAX do skryptu PHP. Wydaje się, że nie ma żadnego problemu z wartościami tekstowymi typu wejściowego, ale gdy próbuję dołączyć pliki, pojawia się błąd TypeError: Wartość nie implementuje interfejsu FormData.Javascript: TypeError: Wartość nie implementuje interfejsu FormData

Jestem nowy w FormData, ale szukałem w Internecie i nie mogłem znaleźć żadnego dokumentu na temat tego błędu.

Oto forma:

<form id="item_form" class="item_form" enctype="multipart/form-data"> 
    <div class=""> 
     <label for="emp_photos">photos</label> 
     <input id="emp_photos" class="inputText" type="file" value="" name="emp_photos"> 
    </div> 
</form> 

oto Javascript:

var formData = new FormData();  
formData.append('photos', $('#emp_photos').files[0]); 

tu jest błąd pojawia się w Firebug

TypeError: Value does not implement interface FormData. 

...igger("ajaxComplete",[N,p]),--b.active||b.event.trigger("ajaxStop")))}return N},... 

jquery....min.js (line 5) 

Co robię źle tutaj?

EDIT: część ajax

$.ajax({ 
    type: 'POST', 
    url: '"; 
    echo $_SESSION["url_base"]; 
    echo "operations/add_employes', 
    data: formData, 
    xhr: function() { // custom xhr 
     myXhr = $.ajaxSettings.xhr(); 
     if(myXhr.upload) { // check if upload property exists 
     myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload 
     } 
     return myXhr; 
    }, 
    success: function(msg) {/*...*/} 

}); 
+1

Gdzie jest rozmowa ajax. Czy możemy to zobaczyć? – KingKongFrog

+0

To wydaje się być właściwym rozwiązaniem http://stackoverflow.com/questions/15259632/upload-image-as-base64-with-jquery-ajax –

+0

Ty Tim, który pomógł mi rozwiązać to :) –

Odpowiedz

2
var inputs = $("input[type=file]"), 
    files = []; 

// jquery or javascript have a slightly different notation 
// it's either accessing functions() or arrays [] depending on which object you're holding at the moment 
for (var i = 0; i < inputs.length; i++){ 
    files.push(inputs.eq(i).prop("files")[0]); 
    //files.push(inputs[i].files[0]); 
    //filename = inputs[i].files[0].name; 
    //filesize = inputs[i].files[0].size; 
} 

if (formdata) { 
    // you can use the array notation of your input's `name` attribute here 
    formdata.append("emp_photos[]", files); 
} 
+0

thnx dużo, to świetnie – bhawin

+0

w jaki sposób "emp_photos []" będzie dostępne w backend php – bhawin

+0

$ _POST ['emp_photos'] foreach ($ _POST ['emp_photos'] jako $ i => wartość $) { echo "emp_photos [$ i] jest wartość $
"; } –

Powiązane problemy