推送标头和 AJAX
我有一个强制下载的脚本,我通过 Javascript 调用它。 但是,该对话框不会弹出,这是 download.php 脚本:
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");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($properFilename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
这是 Javascript(使用 JQuery):
///force download
$.ajax({
type: "GET",
url: "download.php",
data: 'file=' + msg + '&properFilename=' + properFileName,
success: function(msg){
window.location.href = msg;
});//ajax
这会将我的浏览器重定向到另一个页面,而不是显示向下的对话框。
我知道 JS 变量 msg 包含具有正确标题的文件,但我不知道如何处理它才能显示下载对话框。
谢谢大家
。 不知道该线程 JS 或 PHP 放在哪里。
编辑:
我有正确的方法,我确信:) - 用户来到我的网站,他们填写表格,然后按提交。 几秒钟后,他们的文件应该显示在可以下载的对话框中。 为此:
我进行 AJAX 调用来获取文件并下载它。 我使用 PHP 脚本发送标头。 现在我需要的是一种显示下载对话框的方法!
I have a script that forces a download and I make a call to this via Javascript. However, the dialog box doesn't pop up, here is the download.php script:
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");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($properFilename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
Here is the Javascript (using JQuery):
///force download
$.ajax({
type: "GET",
url: "download.php",
data: 'file=' + msg + '&properFilename=' + properFileName,
success: function(msg){
window.location.href = msg;
});//ajax
This redirects my browser to another page rather than showing the down dialog box.
I know the JS variable msg contains the file with the right headers but I don't know what to do with it to get it to display the download dialog box.
Thanks all
p.s. Didn't know where to put this thread JS or PHP.
EDIT:
I have the right approach I am sure of that :) - A user comes to my site, they fill in a form and they press submit. After a few seconds their fle should show up in a dialog box that they can download. To do this:
I make an AJAX call to get the file and download it. I use the PHP script to send the headers. Now all I need is a way to get the dowload dialog box to show up!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它没有显示对话框,因为它是 Ajax 调用。
这就是你的重定向。 我认为这里不需要 ajax 调用,只需使用 href 链接正常调用页面即可。
编辑
如果您希望提交表单并显示下载对话框,请执行以下操作:
It's not showing the dialog box for the very fact that its an Ajax call.
Thats what's redirecting you. I don't think you need an ajax call here, just call the page normally with an href link.
edit
If you want the form to submit and show the dialog box for the download, do this:
如果它完整的 download.php 脚本我无法在其中找到变量 $file (只有 $filename) - 但在 JS 中你发送一个 $file 变量。 第二:JS 中出现问题 - 为什么在
data:
和success:
中使用这样的变量名msg
?If it full download.php script I can't find in it variable $file (only a $filename) - but in JS you send a $file variable. Second: something wrong in JS - why you use such variable name
msg
indata:
and insuccess:
?我的看法是,正如 sktrdie 指出的那样,这可能是预料之中的。 我现在无法测试,但如果您考虑一下:用户本身不会收到有关您正在执行的 AJAX 操作的任何通知。 突然收到这样的消息,会让人很恼火。
我猜想,如果您在该页面上创建一个(甚至是隐藏的?) iframe 并重定向到该下载 URL,您将获得该对话框。 YMMV,HTH。
My take is that this may be expected, as sktrdie notes. I can't test right now, but if you think about it: the user, per se, doesn't get any notification about AJAX stuff you're doing. Having suddenly such a message would be irritating.
I guess that if you create a (even hidden?) iframe on that page and redirect to that download URL that you would get that dialog. YMMV, HTH.
我有一段时间没有编写 php 代码了,所以我不确定你的代码是否有问题。 但我认为问题出在你的方法上。 您需要在框架中加载 download.php,而不是作为对 ajax 调用的响应。 所以基本上你需要在文档中形成一个不可见的 iframe 并将其指向 download.php,并将 get 参数附加到 url 上。
I haven't coded php for a while so I'm not sure if there's anything wrong with your code. But I think the problem is in your approach. You need to load download.php in a frame not as a response to an ajax call. So basically you need to form an invisible iframe in the document and point it to download.php whith the get parameters appended to the url.