upload file con xmlHttpRequest in POST (non si può fare)

Stato
Discussione chiusa ad ulteriori risposte.

narcycs

Utente Silver
20 Maggio 2009
0
0
0
55
Hello, sto cercando di uppare un'immagine con xmlHttpRequest, ma non ci riesco :(

questa è la funzione javascript
(passa il titolo e il file in POST)
(ho definito una funzione $(id) che gettaL'elementoById)

Codice:
function new_item() {
	var http = new XMLHttpRequest();

	var url = "save.php";
	var params = "titolo="+$("titolo").value+
	"&file="+$("file").value; // forse sbaglio qua :(
	
	http.open("POST", url, true);

	// set header
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			$("div_info").innerHTML = http.responseText;
		} else {
			$("div_info").innerHTML = wait; // variabile di attesa
		}
	}
	http.send(params);
}

e questa è la funzione della pagina "save.php" che riceve i dati (e funziona correttamente)
(grazie mille a http://blacklight.gotdns.org/wiki/index.php/PHP)

PHP:
function uppa_file($nome_file) {

	// imposta directory salvataggio
	$directory = "./";

	// controllo del file
	if(!isset($_FILES['file']) || $_FILES['file']['size']==0) {
		return 0;	// up non riuscito, nessun file selezionato
	}

	// controllo
	$ctrl = true;

	// dimensione immagine
	$dimensione = (int)(($_FILES['file']['size'])/1024);

	$ctrl &= $dimensione<$max;		// controlla la dimensione in KB

	// nome temporaneo file uppato
	$sorgente = $_FILES['file']['tmp_name'];

	list($larghezza, $altezza, $tipo, $attributi) = getimagesize($sorgente);

	// dimensione massima
	$x_max = 2400;
	$y_max = 4000;
	$max = 4096;

	$ctrl &= $larghezza<=$x_max;	// controlla la larghezza
	$ctrl &= $altezza<=$y_max;		// controlla l'altezza

	if ($ctrl == false) {
		return 1;	// peso e dimensioni non corretti
	}

	// tipo e dimensione file
	$tipo = $_FILES['file']['type'];
	$x = 200;
	$y = (int)($x*$altezza/$larghezza);

	// nome dell'immagine;
	$thumb_name = $nome_file.'.jpg';

	$thumb = imagecreatetruecolor($x, $y);

	if ($tipo == "image/jpeg") {
		// image/jpeg
		$sorgente = imagecreatefromjpeg($sorgente); 

	} else if ($tipo == "image/gif") {
		// image/gif
		$sorgente = imagecreatefromgif($sorgente);
	}

	// ridimensiona immagine
	imagecopyresized($thumb, $sorgente, 0, 0, 0, 0, $x, $y, $larghezza, $altezza); 

	// crea JPEG
	if (imagejpeg($thumb, $thumb_name, 80)) {
		return true;	// copertina uppata :)
	} else {
		return 2; 	// funzione fallita :(
	}
}

se utilizzo un normale form con un input file (in POST), la cosa funziona, con xmlHttpRequest ho problemi :( e la cosa non funziona...

qualcuno potrebbe illuminarmi? thx E+3 :D
 
RE: upload file con xmlHttpRequest in POST

L'unico modo e' usare un iframe per simulare ajaxosita' in upload, che io sappia.
 
Stato
Discussione chiusa ad ulteriori risposte.