2017-05-20 2 views
0

Ich versuche Array-Dateien mit JS zu senden. Mein Code:Senden von FileList mit Ajax zu PHP-Skript

var formData = new FormData(); 
formData.append("files", files); 

$.ajax({ 
    url: './upload.php', 
    method: 'post', 
    data: formData, 
    processData: false, 
    contentType: false, 
success: function(response) { 
    alert('Files uploaded successfully. '); 
    console.log(response); 
}, 
error: function(jqXHR, textStatus, errorThrown) { 
    console.log(textStatus, errorThrown); 
} 

}); 

In diesem Bild, das Sie die Antwort sehen (rot) von PHP https://beta.ctrlv.cz/mUwx und auch können Sie die Dateien Array-Daten sehen. Mein PHP-Code ist:

<?php 
    echo $_POST['files'][0]["name"]; 
?> 

ich PHP-Skript will für den Upload verwenden, aber die Ajax nicht das Array der Datei geschickt, die für das Hochladen von Bedeutung ist.

+0

Sicher ist es '$ _POST' und nicht' $ _FILES'? – Xorifelse

+0

Auch, [eine gute Lektüre] (http://stackoverflow.com/documentation/php/2781/security/29134/uploading-files) zum sicheren Hochladen von Dateien in PHP. – Xorifelse

+0

Wenn ich print_r ($ _ FILES) schreibe, ist die Ausgabe leer: "Array ( )" so weiß ich nicht .. Ich werde es lesen, aber erstens muss ich Arbeits-Upload haben, nachdem ich es sichern kann . – Alex

Antwort

0

Hier ist eine Antwort, die ich gefunden:

var data = new FormData(); 
jQuery.each(jQuery('#file')[0].files, function(i, file) { 
    data.append('file-'+i, file); 
}); 

Sie FormData bezwecken So, jetzt bereit zusammen mit dem XMLHttpRequest gesendet werden.

jQuery.ajax({ 
    url: 'php/upload.php', 
    data: data, 
    cache: false, 
    contentType: false, 
    processData: false, 
    type: 'POST', 
    success: function(data){ 
     alert(data); 
    } 
}); 

Hier ist die Quelle: https://stackoverflow.com/a/5976031/7282094

Hoffnung, die helfen.

+0

Uncaught TypeError: Kann Eigenschaft 'Dateien' von undefined nicht lesen bei uploadFiles (index.php? Dir = u_tchosniper: 342) bei HTMLButtonElement.onclick (index.php? Dir = u_tchosniper: 238) – Alex

+0

Heute versuche ich, den Code zu beheben und seine Arbeit, vielen Dank. – Alex

0

Ändern Sie möglicherweise contentType: false, zu contentType: "multipart/form-data",.

Entnommen http://api.jquery.com/jquery.ajax/

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') Type: Boolean or String When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

+0

das Ergebnis ist von print_r ($ _ POST) und $ _FILES ist "Array()". – Alex