La mia funzione javascript carica solo file di testo correttamente. Qualcuno può aiutarmi a capire come farlo accettare anche le immagini, ecc.?JavaScript non sta caricando dati binari
function fileUpload(files) {
if (!files.length) {
fileList.innerHTML = "<p>No files selected!</p>";
} else {
var list = document.createElement("ul");
for (var i = 0; i < files.length; i++) {
//Set vars
var file = files[i],
fileName = file.name,
fileSize = file.size,
fileData = file.getAsBinary(),
boundary = "xxxxxxxxx",
uri = "receive.php",
//Create file info HTML output
li = document.createElement("li");
list.appendChild(li);
var info = document.createElement("span");
info.innerHTML = file.name + ": " + file.size + " bytes";
li.appendChild(info);
//Start sending file
var xhr = new XMLHttpRequest();
xhr.open("POST", uri, true);
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary); // simulate a file MIME POST request.
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if ((xhr.status >= 200 && xhr.status <= 200) || xhr.status == 304) {
if (xhr.responseText != "") {
alert(xhr.responseText); // display response.
}
}
}
}
var body = "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name='upload'; filename='" + fileName + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += fileData + "\r\n";
body += "--" + boundary + "--";
xhr.send(body);
}
fileList.appendChild(list);
return true;
}
}
Aggiornamento: Ho trovato la seguente funzione on-line su http://code.google.com/p/html5uploader/ ma non riesco a capire come applicarlo alla mia attuale funzione. Xhr.sendAsBinary è l'unica cosa che è cambiata?
// Upload image files
upload = function(file) {
// Firefox 3.6, Chrome 6, WebKit
if(window.FileReader) {
// Once the process of reading file
this.loadEnd = function() {
bin = reader.result;
xhr = new XMLHttpRequest();
xhr.open('POST', targetPHP+'?up=true', true);
var boundary = 'xxxxxxxxx';
var body = '--' + boundary + "\r\n";
body += "Content-Disposition: form-data; name='upload'; filename='" + file.name + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += bin + "\r\n";
body += '--' + boundary + '--';
xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
// Firefox 3.6 provides a feature sendAsBinary()
if(xhr.sendAsBinary != null) {
xhr.sendAsBinary(body);
*snip*
Come recuperare i file. Voglio dire che il parametro del file deve essere fornito da qualche parte. Come si fa a farlo? Ho bisogno di un po 'di spiegazione su questo argomento del caricamento di file tramite ajax. Qualsiasi aiuto sarà molto apprezzato. – user1575229
@ user1698985 Non sono sicuro di cosa intendi, vuoi dire recuperarlo dal lato PHP? In tal caso lo puoi trovare nei dati POST. – natli
recuperarlo sul lato server è un'altra discussione. Voglio solo trovare il modo in cui il file viene selezionato sul client caricato nei dati dei moduli e inviato tramite Ajax. In Html5 è facile. ma non capisco come sia nelle versioni precedenti di html. Ho provato a dare un'occhiata al plug-in ajaxform, ma è difficile capirlo da come sta recuperando i dati dei file e inviando tramite ajax. Voglio capire l'intera procedura, dalla selezione del file alla gestione in jQuery e all'invio al server. – user1575229