Ho un file php che crea un file .txt e lo fa scaricare. Se apro il file .php direttamente, il download va a buon fine (commento gli if per gli isset e passo i valori brutalmente).
Io devo far partire il download del file alla pressione di un bottone. Tale download non parte. Utilizzo jquery ma viene chiamata la funzione "error", il cui xhr.responseText mostra il contenuto del file che dovrebbe essere scaricato! (Tranne per il fatto che i "tab" nel file vengono mostrati come quadratini).
Codice jquery (ho provato anche con la .post abbreviata)
codice:
$("#experimenter_dataset").click(function() {
var imei = $("#select_download_experimenters option:selected").text();
$.ajax({
type: "POST",
url: "download_imei_activities.php",
contentType: "application/x-www-form-urlencoded",
dataType: "json",
data: {imei:imei},
success: function (data) {
alert(data);
},
complete: function () {
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
Questo è il file .php
codice:
try {
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
if (isset($_POST["imei"])) {
$imei = $_POST["imei"];
$stmt = $pdo->prepare("SELECT * FROM performedactivity WHERE experimenter=:imei");
$stmt->bindParam(":imei", $imei, PDO::PARAM_STR);
$output = "";
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$activity = $row["activity"];
$timestamp_start = $row["timestampstart"];
$timestamp_stop = $row["timestampstop"];
$date_hour_start = explode(" ", $timestamp_start);
$date_hour_stop = explode(" ", $timestamp_stop);
$output .= $date_hour_start[0] . "\t" . $date_hour_start[1] . "\t" . $activity . "START" . "\r\n";
$output .= $date_hour_stop[0] . "\t" . $date_hour_stop[1] . "\t" . $activity . "END" . "\r\n";
}
} else {
echo json_encode($pdo->errorInfo());
}
// We'll be outputting a text file
header('Content-type: text/plain');
// It will be called report.txt
header('Content-Disposition: attachment; filename='.$imei . ".txt");
print($output);
} else {
echo json_encode($pdo->errorInfo());
}
} catch (PDOException $pe) {
die($pe->getMessage());
}
$pdo = null;
Non riesco proprio a far partire sto download..
Update: l'unica cosa che sono riuscito a fare giocando con li header e con readfile, è far comparire il file nella cartella di altervista dove ho gli script -.-
Update 2: ora va in success ma il download comunque non parte.
Update 3:
Sto riprovando così:
JQUERY
codice:
$("#experimenter_dataset").click(function() {
var imei = $("#select_download_experimenters option:selected").text();
$.post("download_imei_activities.php", { imei:imei }).done(function( data ) {
});
});
Php come prima tranne per..
codice:
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename='.$imei . ".txt");
header("Content-Transfer-Encoding: binary");
print($output);