In this example we have a sample generic handler that reads a text file and resturns its content. In your case it would generate and return pdf file
The Handler:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Script.Serialization
Imports System.Web.Caching
Imports System.IO
Imports System.Diagnostics
Public Class genHandler
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim testData As String = ""
Dim serializer As New JavaScriptSerializer()
Dim sbJsonResult As New StringBuilder()
testData = getTestData(context)
serializer.Serialize(testData, sbJsonResult)
context.Response.Clear()
context.Response.ContentType = "application/json; charset=utf-8"
context.Response.Write(sbJsonResult.ToString())
End Sub
''' <summary>
''' read text file and encode it as html
''' </summary>
''' <param name="context">HttpContext, HTTP specific information of the current request</param>
''' <returns>HTTP encoded string from a test text file</returns>
''' <remarks></remarks>
Private Function getTestData(ByVal context As HttpContext) As String
Dim data As String = ""
Dim rootPath As String = context.Server.MapPath("~")
Debug.WriteLine(String.Format("{0}{1}{2}", vbCrLf, "***", vbCrLf))
Debug.WriteLine(String.Format("{0}", rootPath))
If context.Request.QueryString("search") = 1 Then
Dim sr As New StreamReader(Path.Combine(rootPath, "/TestData/1.txt"))
data = sr.ReadToEnd()
data = HttpUtility.HtmlEncode(data)
ElseIf context.Request.QueryString("fileName") = 1 Then
Dim sr As New StreamReader(Path.Combine(rootPath, "/TestData/2.txt"))
data = sr.ReadToEnd()
data = HttpUtility.HtmlEncode(data)
Else
data = "file not found on server."
End If
Return data
End Function
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Default.aspx page
<html>
<head>
<title></title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/Script.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="selectOption">
<option value="1">File One</option>
<option value="2">File Two</option>
</select>
<input type="submit" value="Get Data" onclick="javascript:loadDynamicContent();" />
<div id="divLoading">
Loading...
</div>
<div id="divResult">
</div>
</div>
</form>
</body>
</html>
Script.js file
function loadDynamicContent() {
scroll(0, 0);
showProgress();
var fileName = $('#selectOption').val();
alert(fileName);
$.getJSON(
'BLL/genHandler.ashx?fileName' + fileName,
function (data) {
hideProgress();
$("#divResult").css("display", "block");alert(data);
$("#divResult").slideDown("slow");
$("#divResult").html(data.d);
alert(data);
}
);
}
function showProgress() {
$("#divLoading").css("display", "block");
}
function hideProgress() {
$("#divLoading").css("display", "none");
}
NOTE: Make sure you update the script and jquery path when you test the code above.
Use
VB to C#[
^] online conversion tool if you like to have it in C#
Hope this helps.