这似乎是一个关于noobs的问题,但我想确切地知道当用户点击下载按钮时开始下载需要什么代码。假设我有这个网页和一个包含用户设置的过滤器(即选择日期范围)的表单,当他点击下载按钮时,在他的硬盘上创建了一个csv文档。我必须与哪个模块进行通信才能启动下载,文件具有此扩展名并包含X数据。我听说你必须在HTTP标题中设置一些字段,但我不太清楚我应该怎么做。
编辑:文件下载现在可以使用,但我的文件现在包含以前在页面上写的所有html以及要下载的数据。这是我的代码片段。为了便于阅读,我删除了很多语句
# Handler class which handles the page /Download
class downloadsHandler(webapp2.RequestHandler):
def get(self):
# write download page on the browser
template = JINJA_ENVIRONMENT.get_template('/pages/download.html')
self.response.write(template.render(template_values))
# data to download
buf = getLatestdata()
# size of data
size = sys.getsizeof(buf)
# set HTTP headers to notify server of download
self.response.headers["Content-Type"] = "text/csv"
self.response.headers["Cache-Control"] = "no-cache, must-revalidate"
self.response.headers["Content-Disposition"] = "attachment; filename=kioskData.csv"
self.response.headers["Content-Length"] = size
self.response.headers["Content-Transfer-Encoding"] = "binary"
# generate download
self.response.write(buf)
如何告诉浏览器只包含要下载的数据?
答案 0 :(得分:1)
你应该让下载按钮指向一个PHP文件(如果使用PHP),它将以下行作为标题
<?php
header('Content-type: text/csv');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-Disposition: attachment; filename="filename.csv"');
?>
希望这有帮助!
更新:动态添加示例CSV文件
<?php
echo "This is a CSV file generated by PHP code on the fly";
// Let's print the column headers on first row
echo "ID,Name,Type\r\n";
echo "1,John,1\r\n";
echo "2,Doe,2\r\n";
header('Content-type: text/csv');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-Disposition: attachment; filename="filename.csv"');
?>
答案 1 :(得分:1)
我使用以下内容下载和查看文件(支持的位置)。此代码支持在用户设备上重命名文件。
<?php
getFile("test.csv");
function getFile($file, $mode="download", $outfile=""){
// File Exists?
if (file_exists($file)){
// Parse Info / Get Extension
$fsize = filesize($file);
$path_parts = pathinfo($file);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "jpg": $ctype="image/jpg"; break;
case "csv": $ctype="text/csv"; break;
// video
case "3gp": $ctype='video/3gpp'; break;
case "3g2": $ctype='video/3g2'; break;
case "avi": $ctype='video/avi'; break;
case "mp4": $ctype='video/mp4'; break;
case "ogv": $ctype='video/ogg'; break;
case "asf": $ctype='video/asf'; break;
case "mov": $ctype='video/quicktime'; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
$outfile = ($outfile=="" ? basename($file) : basename($outfile));
if ($mode == "view"){
// View file
header('Content-Disposition: inline; filename='.$outfile);
}
else {
// Download file
header('Content-Disposition: attachment; filename='. $outfile);
}
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
if (ob_get_length() > 0 ) {
ob_clean();
flush();
}
readfile( $file );
}
else {
echo('File Not Found: ' . $file);
}
}
?>
答案 2 :(得分:0)
确定。所以现在一切正常。我必须清除包含当前页面的html数据的输出流
self.response.clear()
在设置标题之前。 这将生成一个仅包含缓冲区内容的csv文件