2

MVC3を使用して特定のリンクがクリックされたときに、byte[]をストリーミングしてjqueryモーダル内にロードしようとしています。私のコントローラーには

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

私の見解の1つで私は持っています

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

これにより、PDFファイルがタブで開きます。しかし、私はbyte[]をモーダル内のpdfファイルとして開きたいと思います。

ヘルプはありますか?

4

2 に答える 2

3

Iframeがあなたの答えかもしれません。

問題は、ajaxが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>
于 2013-03-18T17:01:51.220 に答える
0

これが[PDFを表示/印刷/保存する]ソリューションであり、MVC-ajax呼び出しを介してbyte[]からPDFモーダルダイアログを生成します。それは他の場所にある他の多くの半関連の投稿から構築されています。2つのオプションがあります。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;
  }//___________________________________
于 2017-06-19T12:27:45.777 に答える