Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Ajax和php中自动下载文件_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 在Ajax和php中自动下载文件

Javascript 在Ajax和php中自动下载文件,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个提示输入密钥的表单。当用户插入密钥并单击submit时,它将在db中搜索该密钥。如果找到密钥,则必须自动下载与密钥相关的文件。我已经使用AJAX和php完成了db的验证。现在我想自动下载文件。我该怎么做?(如果我能在php文件中自动下载会更好) JQUERY $('#review-submit').on('click',function(){ var $secret_pin= $('#pin').val(); jQuery.ajax({

我有一个提示输入密钥的表单。当用户插入密钥并单击submit时,它将在db中搜索该密钥。如果找到密钥,则必须自动下载与密钥相关的文件。我已经使用AJAX和php完成了db的验证。现在我想自动下载文件。我该怎么做?(如果我能在php文件中自动下载会更好)

JQUERY

$('#review-submit').on('click',function(){
        var $secret_pin= $('#pin').val();
        jQuery.ajax({
              type : 'post',
              url : ajax_review_plan_object.ajaxurl,
              data : {
                  action : 'loadReviewPlansByAjax',
                  'search': $secret_pin
              },
              contentType: "application/x-www-form-urlencoded; charset=UTF-8",

              success: function(response){
                console.log(response);
                response= response.match(/\d{2}$/);
                if(response==1){
                  $('#empty_pin').fadeIn();
                  $('#invalid_pin').fadeOut();
                  $('#correct_pin').fadeOut();
                  $('#empty_table').fadeOut();
                }else if(response==-1){
                  $('#empty_table').fadeIn();
                  $('#invalid_pin').fadeOut();
                  $('#correct_pin').fadeOut();
                  $('#empty_pin').fadeOut();
                }else if(response==2){
                  $('#correct_pin').fadeIn();
                  $('#invalid_pin').fadeOut();
                  $('#empty_table').fadeOut();
                  $('#empty_pin').fadeOut();
                }else if(response==3){
                  $('#invalid_pin').fadeIn();
                  $('#correct_pin').fadeOut();
                  $('#empty_table').fadeOut();
                  $('#empty_pin').fadeOut();
                }
              }
            });
      });
php


您必须创建一个PHP文件来验证密钥并启动下载流。下面是我使用键on
GET
方法的示例

if(isset($_GET["key"])){
    $key = $_GET["key"];
    //get key from database

    //Validate the key
    if($key == "key-from-database"){
        if(file_exists("/path/to/file/" . $filename)){
            $o = fopen("/path/to/file/" . $filename, "rb");

            header("Content-Type: "); //Put mime type if you have it. You may left it empty
            header('Content-Disposition: attachment; filename="'. $filename .'"'); //File name is important
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0, max-age=0');
            header("Content-Transfer-Encoding: binary");
            header('Pragma: public');

            echo stream_get_contents($o);
            fclose($o);
        }
    }
}

您还可以使用其他的
fopen()

例如,您可以这样做:

  • 进行ajax调用,并以JSON格式返回结果,状态为suces,链接为href
  • 如果状态为success->window.location.replace(链接)或window.open(链接,空白);如果它应该在一个新选项卡中打开->这将重定向用户到该文件

  • 正如您所说的,您可以进行ajax调用并验证它,我不打算发布关于如何实现这一点的代码。

    向我们展示您的代码以及您迄今为止所做的尝试。。你已经在谷歌上搜索过如何下载文件了吗?这应该是在这里发布之前的第一步。我已经在谷歌花了大约3个小时。我正在使用wordpress,我想用php的方式修复它。可能会重复我的代码更新的问题。请参考我的情况,这是一个动态url。当我应用到你的代码时,我得到了一些控制台值,如方框和一些字母,如加密。谢谢你的回答,先生。当我这样做时,我将无法获得返回值并在我的表单中显示/隐藏错误字段。因此,我希望php自动下载我不确定这是否可行,因为如果您要下载文件,您必须按照上面的答案进行下载,但您将需要使用标题,这将导致您在下面评论的问题。。但它不适用于我的第二个选项(在新选项卡中打开)?如果那样行的话,你仍然可以做任何你需要做的事。听起来我必须改变一切。我会试试的,先生。谢谢。这太棒了。我改变了一切。但我仍然有一个问题,我想自动下载文件。window.location.replace和window.open都没有按我的预期工作。我还有别的办法吗?你到底期望什么?我在windows上试过。打开(“,”空白“);而且它工作得很好。。它很快会打开另一个选项卡->开始下载->关闭选项卡,在主页上你可以做任何你喜欢的事情。。
    if(isset($_GET["key"])){
        $key = $_GET["key"];
        //get key from database
    
        //Validate the key
        if($key == "key-from-database"){
            if(file_exists("/path/to/file/" . $filename)){
                $o = fopen("/path/to/file/" . $filename, "rb");
    
                header("Content-Type: "); //Put mime type if you have it. You may left it empty
                header('Content-Disposition: attachment; filename="'. $filename .'"'); //File name is important
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0, max-age=0');
                header("Content-Transfer-Encoding: binary");
                header('Pragma: public');
    
                echo stream_get_contents($o);
                fclose($o);
            }
        }
    }