0

尝试下载由发布的数据动态生成的文件。我有结果回来了。我只是不确定从服务器返回信息(流)后如何将结果保存到本地的正确文件中。

这是ajax调用:

function download_pdf(){
      alert("made it" + result_array); 
      $.ajax({
                type: "POST",
                url: "report_generation/downlaod_pdf.php",
                data: { result_array: result_array },

                success: function(results){
                    alert(results);
                },
                error: 
                    function(xmlHttpRequest, status, error){
                        alert(xmlHttpRequest + "| ajax failure: could not download haq report | " + status + " | error:" + error);
                        var xmlHttpRequestStr = "";
                        for(var x in xmlHttpRequest)
                            xmlHttpRequestStr = xmlHttpRequestStr + xmlHttpRequest[x];
                        alert(xmlHttpRequestStr);
                }
           });
}

像这样从超链接调用此函数,并且成功方法正在返回...

<a class="haq_button" href="javascript:download_pdf()"><span>Download Report As PDF</span></a>

更新:这是 download_pdf 文件生成中的代码片段

// set the headers, prevent caching
            header("Pragma: public");
            header("Expires: -1");
            header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
            header("Content-Disposition: attachment; filename=\"$file_name\"");

            // set appropriate headers for attachment or streamed file
            if ($is_attachment) {
                    header("Content-Disposition: attachment; filename=\"$output_file_name\"");
            }
            else {
                    header('Content-Disposition: inline;');
                    header('Content-Transfer-Encoding: binary');
            }

            // set the mime type based on extension, add yours if needed.
            $ctype_default = "application/octet-stream";
            $content_types = array(
                    "exe" => "application/octet-stream",
                    "pdf" => "application/pdf",
                    "zip" => "application/zip",
                    "mp3" => "audio/mpeg",
                    "mpg" => "video/mpeg",
                    "avi" => "video/x-msvideo",
            );
            $ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default;
            header("Content-Type: " . $ctype);

            //check if http_range is sent by browser (or download manager)
            if(isset($_SERVER['HTTP_RANGE']))
            {
                list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
                if ($size_unit == 'bytes')
                {
                    //multiple ranges could be specified at the same time, but for simplicity only serve the first range
                    //http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
                    list($range, $extra_ranges) = explode(',', $range_orig, 2);
                }
                else
                {
                    $range = '';
                    header('HTTP/1.1 416 Requested Range Not Satisfiable');
                    exit;
                }
}
    else
    {
        $range = '';
}

        //figure out download piece from range (if set)
        list($seek_start, $seek_end) = explode('-', $range, 2);

    //set start and end based on range (if set), else set defaults
    //also check for invalid ranges.
    $seek_end   = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
    $seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);

    //Only send partial content header if downloading a piece of the file (IE workaround)
    if ($seek_start > 0 || $seek_end < ($file_size - 1))
    {
        header('HTTP/1.1 206 Partial Content');
        header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
        header('Content-Length: '.($seek_end - $seek_start + 1));
    }
    else
      header("Content-Length: $file_size");

    header('Accept-Ranges: bytes');

    set_time_limit(0);
    fseek($file, $seek_start);

    while(!feof($file)) 
    {
        print(@fread($file, 1024*8));
        ob_flush();
        flush();
        if (connection_status()!=0) 
        {
            @fclose($file);
            exit;
        }           
    }

    // file save was a success
    @fclose($file);
    exit;
4

4 回答 4

2

这是不可能的。只有你能做的就是用那个 PDF url 打开新的浏览器窗口,或者像这样改变当前窗口的位置: window.location = 'report_generation/downlaod_pdf.php';

于 2013-10-30T13:39:16.000 回答
0

您可以创建一个动态表单并提交它,它将打开一个新窗口/选项卡,如果失败,您可以在该页面中显示一条消息。无论如何,处理和跟踪异常的正确方法不在接收方,而是在发送方(download_pdf.php)

创建动态表单:

var VerifyForm = document.createElement("form");
VerifyForm.target = "_blank"; //You must specify here your target
VerifyForm.method = "POST";
VerifyForm.action = "download_pdf.php";
var dataInput = document.createElement("input");
dataInput.type = "hidden";
dataInput.name = "mydata"; //var name
dataInput.value = value; //var value
VerifyForm.appendChild(dataInput); //Apend var to form, repeat for n
document.body.appendChild(VerifyForm);
VerifyForm.submit();

如果您仍然以自己的方式尝试它,那是不可能的,我建议您阅读更多关于“Ajax”中大写“A”的含义

于 2013-10-30T13:44:54.147 回答
0

您可以尝试通过在downlaod_pdf.php

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$your_file_name");
header("Content-Transfer-Encoding: binary");
readfile($your_file_content);

编辑:并window.location.href = your_php_script_path在 js 文件中添加

于 2013-10-30T13:45:15.373 回答
0

使用 javascript/ajax 是不可能的。您必须使用常规下载链接,或在您的案例表格中。如果您想在 pdf 生成失败的情况下使用 javascript 进行一些错误处理并且仍然保持相同的页面打开,您可以提交到隐藏的 iframe。

于 2013-10-30T13:36:20.353 回答