2016-06-02 2 views
1

iTextSharp를 사용하여 C# 코드에서 PDF 파일을 만들었습니다. 이것은보기에 내 코드입니다 :링크가없는 PDF 파일을 표시하는 방법

<span style="float:left;text-align:left;">    
       <a class="k-button k-button-icontext k-grid-Patient" id="hrefCheckedPatients" href="#" onclick="getChecked();">Export to PDF</a>&nbsp;    
       <a href="#" id="lnkPdfDownload" style="display:none;" onclick="$(this).hide();">Download Generated PDF</a>    
</span>...and then: 
    $.ajax({ 
       type: "POST", 
       url: "/PatientReport/ExportToPDF", 
       dataType: "json", 
       traditional: true, 
       data: { uniqueIds: ids }, 
       success: function (data) { 
        if (data.success) {      
         $('#lnkPdfDownload').show(); 
         $('#lnkPdfDownload').attr('href', '/PatientReport/DownloadFile' + '?fName=' + data.fName); 
        } else { 
         $('#lnkPdfDownload').hide(); 
        } 

       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
        $('#checkedMsg').text('@ELSORegistry.Resources.Views.Patient.PatientStrings.CheckedError').show(); 
        $('#hrefCheckedPatients').blur(); 
       } 
      }); 

컨트롤러에 내 코드 :

[AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult ExportToPDF(List<String> uniqueIds)  { 

      //step 1: we create a memory stream that listens to the document 
      var output = new MemoryStream();    

      PdfWriter writer = PdfWriter.GetInstance(document, output); 
      writer.CloseStream = false; 
      //Here is the code for generating PDF... 
      //The End of ExportToPDF method: 
      byte[] byteInfo = output.ToArray(); 
      output.Write(byteInfo, 0, byteInfo.Length); 
      output.Position = 0;   


      var fName = string.Format("File-{0}.pdf", DateTime.Now.ToString("s")); 
      Session[fName] = output; 

      return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet); 

     } 
//And the other method in the controller is here: 
public ActionResult DownloadFile(string fName) 
     { 
      var ms = Session[fName] as MemoryStream; 
      if (ms == null) 
       return new EmptyResult(); 
      Session[fName] = null; 
      return File(ms, "application/pdf", fName); 
     } 

그래서, 내가 버튼 "PDF로 내보내기"를 클릭하여 보고서를 생성하고, 그때가 링크를 보여 "다운로드 된 PDF 다운로드", 사용자가 보고서를 보려면 클릭해야합니다. 버튼 클릭만으로 링크를 클릭하지 않고 보고서를 표시하고 싶습니다. 그렇게하는 방법? 사전에 도움을 주셔서 감사합니다.

답변

3

HTML에 iframe을 추가하고 해당 iframe의 src 속성을 DownloadFile URL로 설정하십시오.

<iframe id="myframe" src="" /> 
       . 
       . 
       . 
       success: function (data) { 
        if (data.success) {      
         //maybe show the iframe here on this line 
         $('#myframe').attr('src', '/PatientReport/DownloadFile' + '?fName=' + data.fName); 
        } else { 
         //do something else 
        } 

       } 
       . 
       . 
       . 
+0

! 고마워요! – alenan2013

+0

@ alenan2013 좋아요, 답변을 수락하고 upvote :) –

관련 문제