dpntq48842 2016-12-31 11:28
浏览 10
已采纳

在文件附件下载的同时检索值

I have a form that triggers a file download:

function downloadEnhancedSubtitle ($subtitle,$totalSequences,$filename) {
    // Build string
    $subtitleString = '';
    foreach ($subtitle as $thisSegmentKey => $segment) {
        $sequenceString = $segment->sequence."
";
        $sequenceString .= formatMilliseconds($segment->startTimeInMilliseconds).' --> '.formatMilliseconds($segment->endTimeInMilliseconds)."
";
        if(isset($segment->textLine1)) $sequenceString .= utf8_decode($segment->textLine1)."
";
        if(isset($segment->textLine2)) $sequenceString .= utf8_decode($segment->textLine2)."
";
        if(isset($segment->textLine3)) $sequenceString .= utf8_decode($segment->textLine3)."
";
        $sequenceString .= "
";
        $subtitleString .= $sequenceString;
    }
    $subtitleString .= ($totalSequences+1)."
99:99:90,000 --> 99:99:99,999
Enhanced
";

    // Download string
    header("Content-Type: text/plain;charset=windows-1252");
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header("Content-Length: " . strlen($subtitleString));
    echo $subtitleString;
}

The user submits a subtitle file and it is "optimized" on the server and sent back to the user as an attachment download. But I would like at the same time (same form view where the file is downloaded) to trigger a modal with some data of the process, for example, how many lines where optimized.

As "Content-Disposition: attachment" automatically downloads everything printed on screen, is there any way I could retrieve the value of a variable using that response? Maybe changing everything to be an ajax request? (using PHP on the backend)

  • 写回答

1条回答 默认 最新

  • dqsa17330 2016-12-31 12:15
    关注

    Firstly you need to save file on server then you can get response with all variables, but it's one problem - you need to delete all generated files for safety.

    function downloadEnhancedSubtitle ($subtitle,$totalSequences,$filename) 
    {
        [...]
        // Response for ajax
        echo json_encode(
            [
                'filename' => $filename,
                'link' => 'http://localhost/uploads/' . $filename,
                'subtitle' => $subtitleString
            ]
        );
    }
    

    Javascript (jQuery)

    You are getting all your variables from server.

    $.ajax({
        [...],
        success: function(response) {
            // if response is not object
            var obj = JSON.parse(response),
                filename = obj.filename,
                link = obj.link,
                subtitle = obj.subtitle;
    
            // if response is object
            var filename = response.filename,
                link = response.link,
                subtitle = response.subtitle;
    
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题