Thursday, November 24, 2016

Download EXCEL file as a BLOB object in the client side


Here is the code snippet where the excel file object from the server can be downloaded as a blob at the client side.


        var xhr = new XMLHttpRequest();
        xhr.open('POST', Config.RemoteServerUrl, true);
        xhr.responseType = 'arraybuffer';
        xhr.onload = function () {
            if (this.status === 200) {
 
                var filename = inFileName; // "ExportResults.xlsx";
                var disposition = xhr.getResponseHeader('Content-Disposition');
                var type = xhr.getResponseHeader('Content-Type');
                var blob = new Blob([this.response], { type: type });
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the
// blob for which they were created.These URLs will no longer resolve as the data backing the URL has been freed."
                    window.navigator.msSaveBlob(blob, filename);
                } else {
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);
                    if (filename) {
                        // use HTML5 a[download] attribute to specify filename
                        var a = document.createElement("a");
                        // safari doesn't support this yet
                        if (typeof a.download === 'undefined') {
                            window.location.href = downloadUrl;
                        } else {
                            a.href = downloadUrl;
                            a.download = filename;
                            document.body.appendChild(a);
                            a.click();
                        }
                    } else {
                        window.location.href = downloadUrl;
                    }
             setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
             }
           }
        };
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.send(dataToGo); 
    }
 
 
Happy Coding.
 
Cheers
Adam

XML to JSON tricks using C#.NET and Newtonsoft JSON Library


// Converting XML to JSON
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Temp\\SampleData.xml");    // Use this line to load XML from file.           
doc.LoadXml(fullXmlContent); // Use this line if there is already XML content in a string.
string jsonText = JsonConvert.SerializeXmlNode(doc);
JObject dataFromSQL = (JObject) JsonConvert.DeserializeObject(jsonText);
 
// At this point the XML is converted to JSON object.
 
//Get Agency Data from JSON object. Assuming "Agency" element is contained inside "MajorAccounts" element.
 
if (dataFromSQL["MajorAccounts"]["Agency"] == null)
throw new Exception("Data missing in XML for [MajorAccounts][Agency].");
 
JObject agencyJObject = (JObject)dataFromSQL["MajorAccounts"]["Agency"] ;
               
Hope this helps !
 
Cheers
Adam

All Blogs so far ...