流一字节[]以一个jquery模态内装入作为PDF(MVC3)(stream a byte[] to

2019-07-21 20:52发布

我想流一个byte []使用MVC3点击某个链接时,一个jQuery模式里面加载。 在我的控制器我有

public ActionResult GetTermsAndCondtion()
        {
            byte[] termsPdf = GetTermsAndConditions(DateTime.Now);              
            return new FileContentResult(termsPdf, "application/pdf");
        }

在我看来,一个我

@Html.ActionLink("Terms and Conditon","GetTermsAndCondtion","Customer", new{id="terms"})

这将打开的选项卡中的PDF文件。 但我想打开的byte []作为一个模式里面的PDF文件。

任何帮助吗?

Answer 1:

IFRAME可能是你的答案。

问题那就是阿贾克斯无法加载PDF它在浏览器中,展现您必须指定的内容处置PDF格式,并在浏览器里显示他PDF

到所指定的内容添加处置头

HttpContext.Response.AddHeader("content-disposition", "inline; filename=MyFile.pdf")
Return File(fileStream, "application/pdf")

调节器

public ActionResult GetTermsAndCondtion()
        {
            byte[] termsPdf = GetTermsAndConditions(DateTime.Now);
            HttpContext.Response.AddHeader("content-disposition", "inline; filename=MyFile.pdf");

            return File(termsPdf, "application/pdf");
        }

最后加入你的模式这里面的iframe

<iframe src="@url("GetTermsAndCondtion","NameOfYourController")" width="400" height="500" scrolling="auto" frameborder="1">
</iframe>


Answer 2:

这里的一个溶液[查看/打印/保存PDF],即通过MVC的AJAX调用生成从一个字节的[] PDF模态对话框。 它是由许多其他地方的半相关帖子的建造。 有两个选项:PrintDialog1使用对象标记,分别为PrintDialog2使用iframe标签。

控制器>>

[Post]
public ActionResult DoSomethingThatResultsInCreatingAPdf(CreatePaymentViewModel model)
{
  byte[] pdf = DoSomethingThatResultsInCreatingAPdfByteArray();
  string strPdf = System.Convert.ToBase64String(pdf);
  var returnOjb = new { success = true, pdf = strPdf, errors = "" ...otherParams};
  return Json(returnOjb, JsonRequestBehavior.AllowGet);
} 

剃刀页>>

  <div id="PrintPopup"></div>

<script type="text/javascript">

  function DoSomethingThatResultsInCreatingAPdf(btn, event) { 
    event.preventDefault();
    var action = '/Controller/DoSomethingThatResultsInCreatingAPdf/';
    $.ajax({
      url: action, type: 'POST', data: some_input_data,
      success: function (result) {
        if (result.success) {
            PrintDialog-1or2(result.pdf);
        }else{ $('#errors').html(result.errors); }
      },
      error: function () {}
    });
    }//___________________________________

  function PrintDialog1(pdf) { //<object tag>
    var blob = b64StrtoBlob(pdf, 'application/pdf'); 
    var blobUrl = URL.createObjectURL(blob);
    var content = String.format("<object data='{0}' class='ObjViewer'></object>", blobUrl);
    $("#PrintPopup").empty();
    $("#PrintPopup").html(content);
    $("#PrintPopup").dialog({
      open: true, modal: true, draggable: true, resizable: true, width: 800, position: { my: 'top', at: 'top+200' }, title: "PDF View, Print or Save...",
      buttons    : {'Close': function () { $(this).dialog('close'); }}
    });
    return false;
  }//___________________________________

  function PrintDialog2(pdf) { //<iframe tag>
    var content = "<iframe src='data:application/pdf;base64," + pdf + "'></iframe>";
    $("#PrintPopup").empty();
    $("#PrintPopup").html(content);
    $("#PrintPopup").dialog({
      open: true, modal: true, draggable: true, resizable: true, width: 800, position: { my: 'top', at: 'top+200' }, title: "PDF View, Print or Save...",
      buttons    : {'Close': function () { $(this).dialog('close'); }}
    });
    return false;
  }//___________________________________

  String.format = function () {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
      var reg = new RegExp("\\{" + i + "\\}", "gm");
      s = s.replace(reg, arguments[i + 1]);
    }
    return s;
  }//___________________________________

  function b64StrtoBlob(b64Data, contentType, sliceSize) {
    contentType = contentType || '';
    sliceSize = sliceSize || 512;
    var byteCharacters = atob(b64Data);
    var byteArrays = [];
    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
      var slice = byteCharacters.slice(offset, offset + sliceSize);
      var byteNumbers = new Array(slice.length);
      for (var i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }
      var byteArray = new Uint8Array(byteNumbers);
      byteArrays.push(byteArray);
    }
    var blob = new Blob(byteArrays, {type: contentType});
    return blob;
  }//___________________________________


文章来源: stream a byte[] to load inside a jquery modal as PDF (MVC3)