Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.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
Php 从ajax保存文件_Php_Jquery_Ajax_Iframe_Download - Fatal编程技术网

Php 从ajax保存文件

Php 从ajax保存文件,php,jquery,ajax,iframe,download,Php,Jquery,Ajax,Iframe,Download,我想保存在服务器上生成的映像。为此,我使用iframe,但单击后不会出现文件保存对话框。我做错了什么 index.php <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script>

我想保存在服务器上生成的映像。为此,我使用iframe,但单击后不会出现文件保存对话框。我做错了什么

index.php

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $('#onv-save-button').click( function() {
                    $.ajax ({               
                        url: "/ajax.php",
                        type: "POST",
                        success: function(data) {
                            $('#downloadFrame').attr('src' , data);
                        }
                    });
                });
            });

        </script>
    </head>
    <body>
        <iframe src="" id="downloadFrame" ></iframe>
        <button id="onv-save-button">Go!</button>
    </body>
</html>
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $('#onv-save-button').click( function() {
                    $.ajax ({               
                        url: "/ajax.php",
                        type: "POST",
                        success: function(data) {
                            alert(data);
                            $('#downloadFrame').attr('src' , "download.php?file=" + data);
                        }
                    });
                });
            });

        </script>
    </head>
    <body>
        <iframe src="" id="downloadFrame" ></iframe>
        <button id="onv-save-button">Go!</button>
    </body>
</html>

$(文档).ready(函数(){
$(“#onv保存按钮”)。单击(函数(){
$.ajax({
url:“/ajax.php”,
类型:“POST”,
成功:功能(数据){
$('#downloadFrame').attr('src',data);
}
});
});
});
走!
ajax.php

<?  

    // Some actions to generate image
    echo "1.png" ;
?>
<?  
    echo $_SERVER["DOCUMENT_ROOT"]."/1.png";
?>

我做了。这是问题的答案

index.php

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $('#onv-save-button').click( function() {
                    $.ajax ({               
                        url: "/ajax.php",
                        type: "POST",
                        success: function(data) {
                            $('#downloadFrame').attr('src' , data);
                        }
                    });
                });
            });

        </script>
    </head>
    <body>
        <iframe src="" id="downloadFrame" ></iframe>
        <button id="onv-save-button">Go!</button>
    </body>
</html>
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $('#onv-save-button').click( function() {
                    $.ajax ({               
                        url: "/ajax.php",
                        type: "POST",
                        success: function(data) {
                            alert(data);
                            $('#downloadFrame').attr('src' , "download.php?file=" + data);
                        }
                    });
                });
            });

        </script>
    </head>
    <body>
        <iframe src="" id="downloadFrame" ></iframe>
        <button id="onv-save-button">Go!</button>
    </body>
</html>

$(文档).ready(函数(){
$(“#onv保存按钮”)。单击(函数(){
$.ajax({
url:“/ajax.php”,
类型:“POST”,
成功:功能(数据){
警报(数据);
$('#downloadFrame').attr('src',“download.php?file=“+data”);
}
});
});
});
走!
ajax.php

<?  

    // Some actions to generate image
    echo "1.png" ;
?>
<?  
    echo $_SERVER["DOCUMENT_ROOT"]."/1.png";
?>

下载.php

<?php

    $content = file_get_contents($_REQUEST['file']);
    header('Content-Description: File Transfer');
    header("Cache-Control: ");
    header("Pragma: ");
    header("Content-Disposition: attachment; filename=\"".basename($_REQUEST['file'])."\"");

    ob_end_clean();
    ob_start();
    echo $content;
    ob_end_flush();
    exit();

?>


>>echo“1.png”--您是否在downloadFrame元素中看到此文本?您是否知道您正在将“1.png”打印到浏览器输出?单击后,我在downloadFrame中看到我的图像。文件保存对话框的代码在哪里?您所知道的唯一代码是,单击
#onv save按钮后,它会将
1.png
回显到iframe中。您能给出此代码的示例吗?