以PDF(MVC3)格式在jquery模式中加载的字节[]流
我试图在使用MVC3单击某个链接时,在jquery模式中加载一个字节[]。 我的控制器里有以PDF(MVC3)格式在jquery模式中加载的字节[]流,jquery,asp.net-mvc-3,pdf,byte,modal-dialog,Jquery,Asp.net Mvc 3,Pdf,Byte,Modal Dialog,我试图在使用MVC3单击某个链接时,在jquery模式中加载一个字节[]。 我的控制器里有 public ActionResult GetTermsAndCondtion() { byte[] termsPdf = GetTermsAndConditions(DateTime.Now); return new FileContentResult(termsPdf, "application/pdf");
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文件。但是我想在模式中以pdf文件的形式打开byte[]
有什么帮助吗?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>
这里有一个解决方案[查看/打印/保存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;
}//___________________________________
函数doSomethingthatResultingCreatingaPDF(btn,事件){
event.preventDefault();
var action='/Controller/dosomethingthatsresultingcreatingpdf/';
$.ajax({
url:操作,类型:'POST',数据:一些输入数据,
成功:功能(结果){
如果(结果、成功){
PrintDialog-1or2(result.pdf);
}else{$('#errors').html(result.errors);}
},
错误:函数(){}
});
}//___________________________________
函数PrintDialog1(pdf){//
var blob=b64StrtoBlob(pdf,“application/pdf”);
var blobUrl=URL.createObjectURL(blob);
var content=String.format(“,blobUrl);
$(“#PrintPopup”).empty();
$(“#打印弹出”).html(内容);
$(“#打印弹出窗口”)。对话框({
打开:true,模式:true,可拖动:true,可调整大小:true,宽度:800,位置:{my:'top',at:'top+200'},标题:“PDF查看,打印或保存…”,
按钮:{'Close':函数(){$(this).dialog('Close');}
});
返回false;
}//___________________________________
函数PrintDialog2(pdf){//
var-content=“”;
$(“#PrintPopup”).empty();
$(“#打印弹出”).html(内容);
$(“#打印弹出窗口”)。对话框({
打开:true,模式:true,可拖动:true,可调整大小:true,宽度:800,位置:{my:'top',at:'top+200'},标题:“PDF查看,打印或保存…”,
按钮:{'Close':函数(){$(this).dialog('Close');}
});
返回false;
}//___________________________________
String.format=函数(){
var s=参数[0];
for(var i=0;i
如何在jquery模式中调用此返回类型?很抱歉但我是新来的,对此一无所知!添加了代码,请检查它