function openDocumentClick(filename, fileExtension) {

    var fileContent = $('#FileContent').val();

    var contentType;
    switch (fileExtension) {
        case 'png':
            contentType = 'data:image/png;';
            break;
        case 'jpg':
            contentType = 'data:image/jpeg;';
            break;
        case 'jpeg':
            contentType = 'data:image/jpeg;';
            break;
        case 'bmp':
            contentType = 'data:image/bmp;';
            break;
        case 'pdf':
            contentType = 'application/pdf';
            break;
        case 'txt':
            contentType = 'text/plain;charset=utf-8';
            break;
        default:
            contentType = 'application/octet-stream';
            break;
    }

    var blob = base64toBlob(fileContent, contentType);

    saveAs(blob, filename);
}

function base64toBlob(base64Data, contentType, sliceSize) {

    var byteArray,
        byteNumbers;

    contentType = contentType || '';

    var byteCharacters = atob(base64Data);

    // Get blob data sliced or not
    var blobData = sliceSize ? getBlobDataSliced() : getBlobDataAtOnce();

    var blob = new Blob(blobData, { type: contentType });

    return blob;

    /*
     * Get blob data in one slice.
     * => Fast in IE on new Blob(...)
     */
    function getBlobDataAtOnce() {
        byteNumbers = new Array(byteCharacters.length);

        for (var i = 0; i < byteCharacters.length; i++) {
            byteNumbers[i] = byteCharacters.charCodeAt(i);
        }

        byteArray = new Uint8Array(byteNumbers);

        return [byteArray];
    }

    /*
     * Get blob data in multiple slices.
     * => Slow in IE on new Blob(...)
     */
    function getBlobDataSliced() {

        var slice,
            byteArrays = [];

        for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
            slice = byteCharacters.slice(offset, offset + sliceSize);

            byteNumbers = new Array(slice.length);

            for (var i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
            }

            byteArray = new Uint8Array(byteNumbers);

            // Add slice
            byteArrays.push(byteArray);
        }

        return byteArrays;
    }
}