DownloadExport.php
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require_once("RequestManager.php");
require_once("Constants.php");
$op_id = $_REQUEST['id'];
$file_name = $_REQUEST['filename'];
if (empty($op_id))
{
header("HTTP/1.0 400 Bad Request");
echo json_encode(array("success" => false, "error" => "Cannot get operation id"));
exit;
}
//run request
$reqMgr = new RequestManager();
$res = $reqMgr->run('export_result',array('id' => $op_id),OutputTypeEnum::OUTPUT_RESULTFILE);
if (!$res["success"])
{
header("HTTP/1.0 400 Bad Request");
echo json_encode(array("success" => true, "error" => "Error to retrieve exported file"));
exit;
}
//get result file info
$file_info = array();
$file_info["path"] = $res["path"];
$file_info["format"] = $res["format"];
switch($file_info["format"])
{
case "ascii" :
$file_info["extension"] = ".txt";
$file_info["mimetype"] = "text/plain";
break;
case "votable" :
$file_info["extension"] = ".xml";
$file_info["mimetype"] = "application/xml";
break;
case "cdf" :
$file_info["extension"] = ".cdf";
$file_info["mimetype"] = "application/octet-stream";
break;
case "netcdf" :
$file_info["extension"] = ".nc";
$file_info["mimetype"] = "application/octet-stream";
break;
default :
$file_info["extension"] = "";
$file_info["mimetype"] = "text/plain";
}
//set name only if from local browse input mode
if($file_name) {
$file_info["name"] = $file_name.$file_info["extension"];
}else {
$file_info["name"] = "treps_export_".$op_id.$file_info["extension"];
}
//make sure the file exists
if (!is_file($file_info["path"]))
{
header("HTTP/1.0 400 Bad Request");
echo json_encode(array("success" => true, "error" => "Exported file don't exist anymore"));
exit;
}
$file_info["size"] = filesize($file_info["path"]);
//hide notices
@ini_set('error_reporting', E_ALL & ~ E_NOTICE);
//turn off compression on the server
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 'Off');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header('Content-Description: File Transfer');
header("Content-Type: ".$file_info["mimetype"]);
header("Content-Disposition: attachment; filename=\"".$file_info['name']."\"");
header("Content-Length: ".$file_info["size"]);
ob_clean();
flush();
//read file
readfile($file_info["path"]);
exit;
?>