2017-08-25 6 views
0

プロジェクトに取り組んでいて、新しいタブでpdfを表示する必要があります。byte[]としてレポートサービスからpdfを取得するコントローラーがあります。私は、コントローラ呼び出すためのAjaxを使用しています新しいウィンドウでコントローラーから返されたPDFストリームを表示する

var data = Search(search_info); 
var stream = new MemoryStream(data, 0, data.Length, true, true);  
Response.Clear(); 
Response.ClearHeaders(); 
Response.AddHeader("Content-Disposition", "filename=\"\"" + "caseoverview" + ".pdf" + ""); 
Response.ContentType = MimeTypes.ApplicationPdf; 
Response.OutputStream.Write(data1, 0, Convert.ToInt32(stream.Length)); 
Response.Flush(); 
return new FileStreamResult(stream, "application/pdf"); 

function OnCompleteMethod(dataq, status) { 
    if (status === "success") { 

     var w = window.open("data:application/pdf, " + escape(dataq.responseText)); 
     w.document.write(dataq.responseText); 
     w.document.close(); 
} 
} 

そして、これは私が新しいタブで取得していますものです:

@using (Ajax.BeginForm("Search", "Controller", null, 
      new AjaxOptions 
      { 
       HttpMethod = "post", 
       OnComplete = "OnCompleteMethod", 
       OnFailure = "OnFailtureMethod" 
      })) 
     { 

      <div id="query-filter" class="filters"> 
       @Html.Partial("QueryFilter", Model) 
      </div> 
      <div class="row"> 
       <div class="col-md-2 col-sm-6 col-md-offset-8"> 
        <button class="btn btn-submit btn-block" type="submit">Find</button> 
       </div> 
       <div class="col-md-2 col-sm-6"> 
        <button class="btn btn-submit btn-block" type="reset">Reset</button> 
       </div> 
      </div> 
     } 

と成功の方法のためのjavascriptです:

enter image description here

誰かがこの問題を手伝ってくれて、私が間違ってやっていることや解決策に欠けていることを説明することができますか?

ありがとうございました。

答えて

0

私の代わりに、私はちょうどこのjavascript関数ましたAjax呼び出し使用の異なるアプローチの問題、解決:

function reportPrinter() { 
var myParamether= $('#paramether').val(); 
window.open('/MyController/ReportPrinterMethod?report=reportview&paramether=' + myParamether, 'ReportView', ''); 

}

とコントローラで、私はこのアプローチのための方法を変更します:

[HttpGet] 
    public ActionResult ReportPrinterMethod(string myParamether) 
    { 
     if (!ModelState.IsValid) return View(); 

     var data = GetDataReport(myParamether); //Return a byte[] with the pdf content. 

     Response.AddHeader("Content-Disposition", "filename=\"\"" + "reportview" + ".pdf" + ""); 
     Response.ContentType = "application/pdf"; 
     Response.OutputStream.Write(data, 0, Convert.ToInt32(data.Length)); 
     Response.Flush(); 
     Response.Close(); 
     return View(); 
    } 

これで問題が解決しました。

関連する問題