2017-08-25 38 views
0

我正在開發一個項目,我需要在新選項卡中顯示pdf,我有一個控制器,它從報告服務中獲取pdf作爲byte[]在新窗口中顯示從控制器返回的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"); 

我使用Ajax調用控制器:

@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是:

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(); 
} 
} 

這是我在新標籤我得到:

enter image description here

有人可以幫助我解決這個問題,並向我解釋我做錯了什麼或者我在解決方案中缺少什麼。

非常感謝。

回答

0

我解決這個問題有不同的方法,而不是使用Ajax調用我也只是這個javascript函數:

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(); 
    } 

而且解決了這個問題。