2016-12-31 46 views
0

我有一個觸發文件下載表格:檢索值

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

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

用戶提交的字幕文件,並在服務器上的「優化」,併發送回用戶作爲附件下載。但是,我希望在同一時間(相同的窗體視圖,下載文件的位置)觸發一個包含該過程的一些數據的模式,例如,優化了多少行。由於「Content-Disposition:attachment」自動下載屏幕上打印的所有內容,有沒有什麼方法可以使用該響應檢索變量的值?也許改變一切是一個Ajax請求? (在後端使用PHP)

回答

0

首先,您需要將文件保存在服務器上,然後您可以獲得所有變量的響應,但這是一個問題 - 您需要刪除所有生成的文件以確保安全。

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

的Javascript(jQuery的)

您是從服務器獲取所有變量。

$.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; 

    } 
}); 
+0

謝謝!還有一個問題,如果我發送鏈接下載文件,我需要文件名還是subtitleString? – wallypg

+0

您需要文件名,因爲它是放置在您保存文件的服務器上的鏈接。 –