ajax / php文件下载无法正常工作?

时间:2022-01-13 09:45:29

I am trying to force download a txt file upon clicking on a button. The content is dynamically generated and is stored in a javascript variable. The download window doesn't appear when clicking the button however the ajax call is successful. What am I doing wrong?

我试图在单击按钮时强制下载txt文件。内容是动态生成的,并存储在javascript变量中。单击按钮时不显示下载窗口,但ajax调用成功。我究竟做错了什么?

The php:

<?php
$Proof = $_REQUEST["Proof"];
$proof = stripslashes($Proof);
$file = 'savedproof.txt';
file_put_contents($file, $proof);
header('Content-disposition: attachment; filename="'.$file.'"');
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Content-Description: File Transfer');

readfile($file);
?>

The javascript:

$("#save").click(function () {

    var proof = $("#main").html();
     $.ajax({ 
             type: 'POST', 
             url: 'save-to-file.php',
             data: {Proof: Proof},
             dataType: "html"

            });
}

Alternatively I tried using window.location.href but I couldn't pass the variable Proof to the php file. I tried something like this:

或者我尝试使用window.location.href,但我无法将变量Proof传递给php文件。我试过这样的事情:

window.location.href ="download.php?Proof="+Proof;

Although the download dialog does appear, only the beginning part of the variable Proof is in the file. I tested both ways on firefox and chrome.

虽然下载对话框确实出现,但只有变量Proof的开头部分在文件中。我在firefox和chrome上测试了两种方式。

3 个解决方案

#1


1  

Your AJAX request isn't working because it isn't triggering browser navigation. It's just feeding the response to Javascript, which is ignoring it. You'll probably need to construct a fake <form> element and submit it to get the results to download.

您的AJAX请求无法正常工作,因为它没有触发浏览器导航。它只是将响应提供给Javascript,而忽略了它。您可能需要构造一个假的

元素并提交它以获取要下载的结果。

As far as window.location.href is concerned, URLs are typically limited to about 2 to 4 KB, so you're getting cut off. So that won't work.

就window.location.href而言,URL通常限制在大约2到4 KB,因此您将被切断。所以那不行。

#2


3  

Javascript can't download files to the client machine due to security concerns.

出于安全考虑,Javascript无法将文件下载到客户端计算机。

Just make the button a link (styled however you want) and do this:

只需将按钮设为链接(无论您想要的样式),然后执行以下操作:

<a id="save" href='/path/to/download.php' target="_blank"></a>

Then have an onready function to change the href based on the the value of #main

然后有一个onready函数来根据#main的值更改href

$.ready(
   var proof = $('#main').html();
   var href = $('#save').attr('href') + '?Proof=' + encodeURIComponent(proof);
   $('#save').attr('href', $href); 
});

No reason for AJAX at all here as far as I can tell.

据我所知,这里根本就没有AJAX的理由。

#3


0  

You can do this by creating and sending form via jquery (page not reloaded):

您可以通过jquery创建和发送表单来执行此操作(页面未重新加载):

$(document).on('click', '#save', function () {
    var proof = $("#main").html();
    var form = $(document.createElement('form'));
    form.attr('action', 'save-to-file.php');
    form.attr('method', 'POST');
    var input = $('<input>').attr('type', 'hidden').attr('name', 'Proof').val(proof);
    form.append(input);
    form.appendTo(document.body);
    form.submit();
    form.remove();
});

#1


1  

Your AJAX request isn't working because it isn't triggering browser navigation. It's just feeding the response to Javascript, which is ignoring it. You'll probably need to construct a fake <form> element and submit it to get the results to download.

您的AJAX请求无法正常工作,因为它没有触发浏览器导航。它只是将响应提供给Javascript,而忽略了它。您可能需要构造一个假的

元素并提交它以获取要下载的结果。

As far as window.location.href is concerned, URLs are typically limited to about 2 to 4 KB, so you're getting cut off. So that won't work.

就window.location.href而言,URL通常限制在大约2到4 KB,因此您将被切断。所以那不行。

#2


3  

Javascript can't download files to the client machine due to security concerns.

出于安全考虑,Javascript无法将文件下载到客户端计算机。

Just make the button a link (styled however you want) and do this:

只需将按钮设为链接(无论您想要的样式),然后执行以下操作:

<a id="save" href='/path/to/download.php' target="_blank"></a>

Then have an onready function to change the href based on the the value of #main

然后有一个onready函数来根据#main的值更改href

$.ready(
   var proof = $('#main').html();
   var href = $('#save').attr('href') + '?Proof=' + encodeURIComponent(proof);
   $('#save').attr('href', $href); 
});

No reason for AJAX at all here as far as I can tell.

据我所知,这里根本就没有AJAX的理由。

#3


0  

You can do this by creating and sending form via jquery (page not reloaded):

您可以通过jquery创建和发送表单来执行此操作(页面未重新加载):

$(document).on('click', '#save', function () {
    var proof = $("#main").html();
    var form = $(document.createElement('form'));
    form.attr('action', 'save-to-file.php');
    form.attr('method', 'POST');
    var input = $('<input>').attr('type', 'hidden').attr('name', 'Proof').val(proof);
    form.append(input);
    form.appendTo(document.body);
    form.submit();
    form.remove();
});