Asp.Net

Generate and Download PDF file on Ajax Post method, C#

Requirements: On ajax post, save data to database, generate invoice and download as PDF file. There are many open source packages available to generate PDF file. So I’m not going describe that using which paid/free package is used to generate PDF file. I am going to describe the trick how can download the generated PDF file without saving on hard drive.

HomeController.CS


public class HomeController : Controller
    {
        public ActionResult Index()
        {         
            return View();
        }

        public ActionResult Download()
        {
            //using (var ms = new MemoryStream())
            //{
                //using (var document = new Document(PageSize.A4, 50, 50, 15, 15))
                //{
                //    PdfWriter.GetInstance(document, ms);
                //    document.Open();
                //    document.Add(new Paragraph("HelloWorld"));
                //    document.Close();
                //}
                //Response.Clear();
                ////Response.ContentType = "application/pdf";
                //Response.ContentType = "application/octet-stream";
                //Response.AddHeader("content-disposition", "attachment;filename= Test.pdf");
                //Response.Buffer = true;
                //Response.Clear();
                //var bytes = ms.ToArray();
                //Response.OutputStream.Write(bytes, 0, bytes.Length);
                //Response.OutputStream.Flush();
           // }

            return View();
        }

        [HttpPost]
        public ActionResult Save()
        {

// write code here to save the data in database. 
            var fName = string.Format("MYPDF-{0}.pdf", DateTime.Now.ToString("s"));
            using (var ms = new MemoryStream())
            {
                using (var document = new Document(PageSize.A4, 50, 50, 15, 15))
                {
                    PdfWriter.GetInstance(document, ms);
                    document.Open();
                    document.Add(new Paragraph("HelloWorld"));
                    document.Close();
                }

                var bytes = ms.ToArray();              
                Session[fName] = bytes;
            }

            return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet);
            //return View();
        }
         public ActionResult DownloadInvoice(string fName)
         {
             var ms =  Session[fName] as byte[] ;
             if(ms == null)
                 return new EmptyResult();
             Session[fName] = null;
             return File(ms, "application/octet-stream", fName);
         }
    }

Index.cshtml

 <input type="button" id="btnSave" value="Save & Download PDF" />
 
 $(document).ready(function () {
 $("#btnSave").click(function () {
 $.ajax({
 type: 'POST',
 url: "/home/save",
 dataType: "json",
 success: function (resultData)
 {
 if (resultData.success) {
 window.location = "/home/DownloadInvoice" + "?fName=" + resultData.fName;
 }
 }
 });
 })
 })
 

On ajax post method, data will save into the database. Here Save is a post method. Check the HomeController.CS file. Generate file and save an array of byte in Session. In JSON result, send the name of file. Ajax post method retrieve the data and using window.location send request to server to download the file. Now, check DownloadInvoice method in HomeController.cs.

Thank you for reading this post.

Most important topics

Static Constructor

  • A static constructor is used to initialize the static data once or performed an action to once only.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • User has no control when static constructor executed. In real life scenario static constructor use when class write the log file.

Is MVC Design Pattern or Architectural Pattern?
MVC is an architectural pattern. ASP.NET MVC is a part of ASP.NET Web application framework.

Caching
Caching is a use to improve performance of website by storing frequently access data and reusing that data.
Advantages: Reduce hosting server round trip. Reduce database server round trips. Reduce network traffic.
Disadvantages: Avoid caching if data unique per user. Avoid caching if database server has not enough RAM. For caching of dynamic contents that change frequently, define a short cache–expiration time rather than disabling caching.

Delegates
A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

Difference between Web Services, WCF and Web API
Web Services

  • It supports only http protocol.
  • It is based on SOAP and return data in XML format only
  • It can be host only on IIS

WCF

  • It supports http, https, tcp, MSMQ protocol.
  • It is based on SOAP and return data in XML format only
  • It can be hosted with in the applicaion or on IIS or using window service
  • WCF Rest support XML, JSON and ATOM data format.

Web API

  • It supports http protocol.
  • It supports MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
  • It can be hosted with in the application or on IIS.
  • WCF Rest support XML, JSON data format.

Components of WCF
There are three main components in WCF.

  • Service Class
  • Hosting Environment
  • End Point

Service Class
Service Contract attribute define saying which application interface will be exposed as a service.

[ServiceContract()]
public interface IServiceGetResult
{
}

Operation Contract
Operation Contract dictates which methods should be exposed to the external client using this service.

[ServiceContract()]
public interface IServiceGetResult
{
[OperationContract()]
decimal GetPercentage(Student model);
}

Data Contract
Data Contract attributes defines which type of complex data will be exchanged between the client and the service. They determine which parameters to be serialized. When you are using simple data types like int, it is not necessary that you need to mark the data contract attribute. Because you will always find matching types on the client. However, complex structure like one shown in the below code snippet you will need to define a data contract. Remember data contract define how this data will be passed during transmission. In short data contract attribute define how data will be serialized will transmission.

As data contract are all about serialization you need to import System.Runtime.Serialization name space.
[DataContract]
public class Student
{
int StudentId {get;set;}
string GrNo {get;set;}
string Name {get;set;}
}
public class serviceGetResult: IServiceGetResult
{
public decimal GetPercentage(Student model)
{
decimal percentage=100;
//get percentage stored procedure call or calculation here.
return percentage;
}
}

Hosting Environment
Self-hosting the service in his own application domain. The service comes in to existence when you create the object of Service Host class and the service closes when you call the Close of the Service Host class.

Host in application domain or process provided by IIS Server.

Host in Application domain and process provided by WAS (Windows Activation Service) Server.

End Points
WCF offers its services to its client using an endpoint. An endpoint comprises of three key elements, the address, binding and contract.

WCF endpoints provide the necessary resources and directions, which help clients to communicate with the various services WCF offers.

We commonly refer the three elements (attributes) as ABC.

01) A stands for Address
02) B stands for Binding
03) C stands for Contract

UK Postcode Validation

Regular expression for JavaScript

var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;

var result = regPostcode.test(SearchTerm);

if (!result) {
alert(“Please enter valid postcode”);
return false;
}

Regular expression for C#

string regPostcode = “([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}”;

System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(regPostcode);

if(!regx.IsMatch(address0.PostalCode))
{
results.Add(new ValidationResult(String.Format(“Please enter valid postcode for {0}.”, addressRank)));
}

You can check the regular expression at http://www.regexplanet.com/

Postal code format and sample postcodes are as below.

Format Example
A9 9AA S1 1AA
A99 9AA M60 1NW
AA9 9AA CR2 6XH
AA99 9AA DN55 1PT
A9A 9AA W1A 1HQ
AA9A 9AA EC1M 1BB

Remove illegal characters from file name or file path in c#

File cannot be saved if file name has illegal characters. So how to remove the illegal characters by a single line code as below. I use LINQ Querty for that.


string fileName = "prakash C/O swami.xml";
fileName = System.IO.Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));

Now the fileName is “rakash CO swami.xml”. The code removes slash ‘/’ because it is a illegal characters. As same any illegal characters will remove using this above code.

How to display Progress bar while page is loading

<pre>

<code>

protected void Page_Load(object sender, EventArgs e)
{
showPageLoad();
//do somthing
    }
private void showPageLoad()
{
int i=0;
Response.Write(“<div id=’divTitle’>Loading(<span id=’message’>0%</span>)</div>”);
Response.Flush();
for(i=5;i<=100;i=i+5)
{
System.Threading.Thread.Sleep(200);// any codes can be typed here
outputflash(i.ToString() + “%”);
Response.Flush();
}
}
private void removePageLoad()
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), “deleteLoading”, “var d = document.getElementById(‘divTitle’);d.parentNode.removeChild(d);”, true);
    }
private void outputflash(string value)
{
Response.Write(“<script type=’text/javascript’>document.getElementById(‘message’).innerHTML='” + value + “‘;</script>”);
    }

</code>

</pre>

Export DataTable to Excel/CSV C#


protected void btnExport_Click(object sender, EventArgs e)
    {
        DataTable dtTable = new DataTable();
        DataRow dtRow;

        dtTable.Columns.Add("SNo", typeof(int));
        dtTable.Columns.Add("Address", typeof(string));

        for (int i = 0; i <= 9; i++)
        {
            dtRow = dtTable.NewRow();
            dtRow[0] = i;
            dtRow[1] = "Address " + i.ToString();
            dtTable.Rows.Add(dtRow);
        }

        Response.ContentType = "Application/x-msexcel";
        Response.AddHeader("content-disposition", "attachment;filename=test.csv");
        Response.Write(ExportToCSVFile(dtTable));
        Response.End();
    }

    private string ExportToCSVFile(DataTable dtTable)
    {
        StringBuilder sbldr = new StringBuilder();
        if (dtTable.Columns.Count != 0)
        {
            foreach (DataColumn col in dtTable.Columns)
            {
                sbldr.Append(col.ColumnName + ',');
            }
            sbldr.Append("\r\n");
            foreach (DataRow row in dtTable.Rows)
            {
                foreach (DataColumn column in dtTable.Columns)
                {
                    sbldr.Append(row[column].ToString() + ',');
                }
                sbldr.Append("\r\n");
            }
        }
        return sbldr.ToString();
    }
}

calling [Ajax.AjaxMethod()] not working

finally its resolved by using,

<system.webServer>
<validation validateIntegratedModeConfiguration=”false” />
<modules>
<add name=”ScriptModule” preCondition=”integratedMode” type=”System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />
</modules>
<handlers>
<remove name=”WebServiceHandlerFactory-Integrated” />

<add verb=”POST,GET” name=”Ajax” path=”ajax/*.ashx” type=”Ajax.PageHandlerFactory, Ajax” />
<add name=”ScriptHandlerFactory” verb=”*” path=”*.asmx” preCondition=”integratedMode” type=”System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />
<add name=”ScriptHandlerFactoryAppServices” verb=”*” path=”*_AppService.axd” preCondition=”integratedMode” type=”System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />
<add name=”ScriptResource” preCondition=”integratedMode” verb=”GET,HEAD” path=”ScriptResource.axd” type=”System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />
</handlers>
</system.webServer>

<system.web>
<httpHandlers>
<add verb=”POST,GET” path=”ajax/*.ashx” type=”Ajax.PageHandlerFactory, Ajax”/>
</httpHandlers>
</system.web>

Country wise TimeZones SQL DB

This time zone list use only for .Net Users.. Because .Net has inbuilt class for converting date-time into UTC and UTC to specific timezone. For that, ‘TimeZone Unique Name’ should be needed which should match with Windows System TimeZone.

You can download sql script from here.

404 error WCF on server

Add below lines undser System.Server / handler section.

<add name="svc-ISAPI-2.0" path="*.svc" verb="*" modules="IsapiModule" scriptProcessor="%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
     <add name="svc-Integrated" path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" resourceType="Unspecified" preCondition="integratedMode" />

   

Import SSL, Auto redirect http to https, Custom Error Page lock violation

Import SSL in IIS 7

For importing SSL certificate, the certificate should have in PKCS #12 format (.pfx). If you have SSL certificate, its not in PKCS #12 format and you have Certificate and Private key you can generate the .PFX SSL certificate using OPENSSL tool or online by click here.

Once the .PFX certificate generated, go to IIS Manager by type ‘inetmgr’ in windows run. Select main not in left pane. And double click on “server certificates” from the middle pane. Then click on ‘import’ from the right pane. A dialog box will open select the SSL certificate which you have generated in .PFX format and enter the password which you had given at the time of generating the PFX certificate. Click on Ok button. Now, you can assign the certificate to your domain, using Binding.

Auto redirect http to https in IIS 7
Method 1 – Using Microsoft URL Rewrite Module

For this method of redirecting from HTTP to HTTPS, you will need to do the following;

1. Install the Microsoft URL Rewrite Module
2. Install your SSL certificate in IIS 7 and bind it to your website
3. Make sure Require SSL is NOT checked under SSL Settings for your website
4. Copy and paste the following code between the and tags in your web.config file in your website root directory.

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
    <conditions>
      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>

5. Test the site by going to http://www.yoursite.com and making sure it redirects

Method 2 – Setting up a Custom Error Page
The second method of setting up an IIS7 redirect HTTP to HTTPS is to Require SSL on the site or part of the site and set up a custom 403.4 error page. To do this, just following these steps:

1. Install your SSL certificate in IIS 7 and bind it to your website
2. In IIS, click on the site name, and go to the SSL Settings section
3. Check Require SSL checked under SSL settings of your website
4. After doing this, users will normally receive this error 403.
5. Create a new text file and paste the following into it:

<html>
<head><title>Redirecting...</title></head>
<script language="JavaScript">
function redirectHttpToHttps()
{
    var httpURL= window.location.hostname + window.location.pathname + window.location.search;
    var httpsURL= "https://" + httpURL;
    window.location = httpsURL;
}
redirectHttpToHttps();
</script>
<body>
</body>
</html>

6. Save the file as redirectToHttps.htm in your C:\Inetpub directory
7. Back in IIS, click on the site name and double-click the Error Pages option
8. Click Add… and enter 403.4 as the Status code. Browse for the redirectToHttps.htm file you just created and click OK
9. Select the error code and press Edit Feature Settings…
10. Click the Custom error pages option and again browse for the redirectToHttps.htm file
11. Test the site by going to http://www.yoursite.com and making sure it redirects

Custom Error Page lock violation

If your IIS 7 web server doesn’t already have it, install the IIS 7.0 Administration Pack from Microsoft.

Navigate to the root web server name in IIS, and open the Configuration Editor (part of the Administration Pack). Change the dropdown to system.webServer/httpErrors, right-click on defaultPath, and choose ‘defaultPath’ Attribute -> Unlock Attribute.

Then try to change the custom error handler page again. Navigate to your site, open Error Pages under the IIS group, click Edit Feature Settings on the right, select Custom error pages and finally, put in your path for the default page.

The Manual Way

I know you can accomplish all this by direct editing in notepad of the appropriate config file on the web server. And that may be required for your particular web hosting environment or company production web server change protocols. But why make it complicated if it doesn’t need to be? Plus, this way you can do it in the GUI, and compare the before and after to see what changes you truly have to make. But if you MUST do it manually, then:

Open the file %windir%\System32\inetsrv\config\applicationHost.config in Notepad. Run Notepad as administrator if you’re having problems.

You’ll see something like this:

<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">

Remove the ,defaultPath section and save.

You will be able to make the changes you need.