2016-12-31 13 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

まず、サーバーにファイルを保存する必要がありますが、すべての変数で応答を得ることができますが、問題は1つあります。安全のためにすべてのファイルを削除する必要があります。

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

ありがとうございました!もう1つの質問ですが、ファイルをダウンロードするためのリンクを送信すると、ファイル名またはsubtitleStringが必要ですか? – wallypg

+0

ファイルを保存したサーバーに置くためのリンクであるため、filenameが必要です。 –

関連する問題