- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用X-SendFile Apache Module从服务器下载大文件。下载效果很好。但是,当下载开始时,我需要向浏览器输出一些文本,例如:“感谢下载文件......”
我的问题是,我无法在一个脚本中同时完成这两件事。我可以下载该文件,然后屏幕上不会输出任何文本。或者,我可以将文本输出到屏幕,但下载脚本将无法启动。
如何在一个 PHP 脚本中完成这两项任务 - 下载文件并将文本内容发送到浏览器/网页?问题可能源于 HTTP 输出 header ,但我不知道如何解决它。
这是我用来通过 X-SendFile
下载文件的示例代码(可以单独正常工作):
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=\"". basename($filePath) . "\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Length: ' . filesize($filePath));
header("X-Sendfile: $filePath");
为了完成,这是我的测试 echo""
字符串。如果将其放置在 X-Sendfile
代码上方,则会获得输出,但不会开始下载。如果将其放在 X-Sendfile
代码下方,则会下载文件,但不会向浏览器回显:
echo "SUCCESS!!!";
ob_flush();
约束:
我需要能够通过 URL 轻松下载该文件。例如,www.mydomain.com/?fileID=asl523n53twteHLfga
。换句话说,PHP 下载脚本位于 index.php
内。当用户输入 www.mydomain.com
时,将显示域登录页面。但是,当传递附加的 $_GET
变量时,PHP 脚本会识别该变量,并应显示 感谢您下载... 而不是登陆页面。
欢迎提供解决方法。重要的是在保持上述约束的同时达到期望的结果。非常感谢你。
最佳答案
如何添加一个单独的下载页面来下载文件(即 download.php?fileID=XYZ
)并让索引页面显示如下消息:
<?php
function homeScreen() {
// your home content
}
function downloadFileScreen ($id) {
?>
<h2>Thank you for your download!</h2>
Your download should start automatically, if it does not, click
<a href="download.php?fileID=<?php print $id; ?>">here</a>
<script type="text/javascript">
window.setTimeout('location.href="download.php?fileID=<?php print $id; ?>"',3000);
</script>
<?php
}
if (isset($_GET['fileID'])) {
$id= $_GET['fileID'];
downloadFileScreen ($id);
?>
} else {
// no ID passed, show home screen
homeScreen();
}
?>
我还没有听说过任何浏览器支持在单个请求中混合下载和 HTTP 内容显示,但我会看看是否可以使用 ob_start()
和 来实现这一点>ob_flush()
.
关于javascript - PHP,X-SendFile : Sending file together with outputting text to the browser (via echo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20247293/
在我的 router/index.js 中,我使用 res.sendfile(..) 如下: var express = require('express'); var router = expres
设置路径的正确方法是什么? 在我的应用程序中,我使用此代码设置发送文件的路径。 app.get('/',function(req, res){//get,put,post,delete res
在我的程序中,我需要检查非阻塞套接字中 sendfile() 操作的完成情况。怎么办? 查了资料,上网查了下,没找到方法 最佳答案 它的工作方式与 send() 非常相似:如果套接字设置为 O_NON
这是我的客户端代码片段: while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, sizeof
这是 LIST ftp 的 cmd 的片段: count = file_list("./", &files); if((fp_list = fopen("listfiles.txt", "w")) =
我正在尝试重新发送一个 Json 文件,但是当我这样做时,它会不断返回许多特殊字符,如下所示: book����mark����8���8���`Q�����������eǾJu¼A����rs/f
我创建文件 1.txt 2.txt 并将一些内容写入 1.txt。 然后我使用下面的代码,想把内容复制到2.txt. 但它不起作用。 2.txt 中没有任何内容。 你能解释一下我的错误吗? int m
我正在尝试实现一个基本的文件服务器。我一直在尝试使用此处的 sendfile 命令:http://linux.die.net/man/2/sendfile我正在使用 TCP。 我可以让它正常发送,但它
我正在使用Wildcard Subdomains在我的 Express 服务器上,这样当用户访问 name.example.com 或从该子域的 API 请求某些内容时,一切都会正常工作。 (导航到
使用res.sendFile()时,响应中不会收到set-cookie header 。 app.get(sessionTracker, (req, res, next) => { res.coo
前奏 sendfile() 是一个非常有用的系统调用,原因有两个: 首先,它比 read()/write() (或 recv())代码更少/send()(如果您更喜欢 jive)循环。 其次,它比上述
服务器端 我尝试从 NodeJs 发送文件 /** * Exports data to PDF format route. */ app.post('/export/pdf', upload.si
我正在尝试使用 Linux 系统调用 sendfile() 来使用线程复制文件。 我有兴趣优化代码的这些部分: fseek(fin, size * (number) / MAX_THREADS, SE
作为服务器,我想从非阻塞客户端套接字读取数据,然后写入文件。 根据 sendfile 的手册页,如果 errno 设置为 EAGAIN,那么这仅表明,如果 output 文件描述符设置为非阻塞,则对
我正在尝试向客户端发送静态 html。 我使用这种方法成功地做到了这一点 app.get('/forgot', function (req, res) { console.log("F
我的/blog 和/post 在浏览器控制台中触发了许多 404 未找到错误,可以在 here 中找到这些错误即使 blog.html 和 post.html 如果我从文件资源管理器打开它都可以工作!
我正在做一个 Node 项目,在其中公开我的公共(public)文件夹,例如: app.use(express.static(path.join(__dirname, '/public'))) 现在,
我正在尝试使用 sendfile() 来实现一个复制程序。 但是当我尝试复制目录时它失败了。目录在 Linux 中不是一种特殊的文件类型吗? 这是我现在使用的代码。它是从 StackOverflow
假设我的 Express.js 应用程序中有这个路由处理程序: app.get('/files/:name', function (req, res) { res.sendfile('path
我正在尝试在 Linux 2.6.32 下测试 sendfile() 系统调用以在两个常规文件之间进行零拷贝数据。据我了解,它应该可以工作:从 2.6.22 开始,sendfile() 已使用 spl
我是一名优秀的程序员,十分优秀!