Thursday, October 11, 2018

Loading PDF using AJAX in an IFRAME


ShareThis
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="file3.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script src="jquery-3.3.1.min.js"></script>
<body>
<form id="form1" runat="server">
<input id="btn" type="button" class="btn btn-inf0" value="Call now" />
<iframe id="if1" src="" width="400"></iframe>
<script>
$("#btn").click(function () {
alert("calling b5");
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
//IE
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
{
$("#if1").attr("src", "PDFGen.aspx");
return;
}
//other browser
var request = new XMLHttpRequest();
request.open("GET", "PDFGen.aspx", true);
request.responseType = "blob";
request.onload = function (e) {
if (this.status === 200) {
var file = window.URL.createObjectURL(this.response);
$("#if1").attr("src", file);
};
};
request.send();
return false;
});
</script>
</form>
</body>
</html>
view raw Default.aspx hosted with ❤ by GitHub
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace file3
{
public partial class PDFGen : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/One.pdf"));
Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition", "inline");
Response.BinaryWrite(bytes.ToArray());
Response.End();
}
}
}
view raw PDFGen.aspx.cs hosted with ❤ by GitHub