Friday 22 September 2017

Create & Download PDF from Byte[] Array using jQuery AJAX and WebMethod

//convert BASE64 string to Byte{} array
function base64ToArrayBuffer(base64) {
    var binaryString = window.atob(base64);
    var binaryLen = binaryString.length;
    var bytes = new Uint8Array(binaryLen);
    for (var i = 0; i < binaryLen; i++) {
        var ascii = binaryString.charCodeAt(i);
        bytes[i] = ascii;
    }
    return bytes;
}

//save Byte[] array and download
function saveByteArray(reportName, byte) {
    var blob = new Blob([byte]);
    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    var fileName = reportName + ".pdf";
    link.download = fileName;
    link.click();
}

//in this case we are exporting a post based on id
function export(id) {
    $.ajax({
        type: "POST",
        url: "Default.aspx/ConvertToPdfAndDownload",
        data: '{postId: "' + id + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            if (response.d == '')
                alert('There is a problem exporting the file');
            else {
                var sampleArr = base64ToArrayBuffer(response.d);
                saveByteArray("File-" + id, sampleArr);
            }
        },
        failure: function (response) {
            alert("Cannot export thefile: Error in calling Ajax");
        }
    });
}


// C# code behind
// using Pdf generator from https://www.nrecosite.com/pdf_generator_net.aspx
[WebMethod]
public static string ConvertToPdfAndDownload(string postId)
{
    string result = "";

    try
    {    
        // convert HTML to PDF Byte[] array and return as Base64 string to AJAX
        string post = "<h2>Here is a test</h2>";
        var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
        result = Convert.ToBase64String(htmlToPdf.GeneratePdf(post));        
    }
    catch (Exception e) { result = e.Message; }
    return result;
}

Thursday 14 September 2017

C# ASP.NET Convert DataTable to CSV and Download

StringBuilder sb = new StringBuilder();
DataTable dt = ds.Tables[0];

IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt.Rows)
{
    IEnumerable<string> fields = row
                                 .ItemArray
                                 .Select(field => string
                                 .Concat("\"", field
                                 .ToString()
                                 .Replace("\"", "\"\""), "\""));
    sb.AppendLine(string.Join(",", fields));
}

Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "inline;filename=export.csv");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "text/csv";
Response.Write(sb);
Response.End();


Dropdownlist with Checkboxes in ASP.NET

Dropdownlist with checkboxes in asp.net more...


// required to make drop down list works with postback and update panel
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    function EndRequestHandler(sender, args) {
        //Binding Code Again
        $('.multi-select').SumoSelect({ okCancelInMulti: true, selectAll: true });
}

Upgrade Windows 11 Home to Windows 11 Pro

Disable internet connection (Wi-Fi, Internet, etc.) Change the product key using the following Generic product key:                     VK7J...