.NET to Javascript compiler

C# code   Html page   Start
public class FileAPIApp
{
    readonly Control info;

    private FileAPIApp()
    {
        info = new Control() { Element = HtmlDocument.Current.GetElementById("info") };

        HtmlInputFileElement fe = (HtmlInputFileElement)HtmlDocument.Current.GetElementById("file");
        if (SupportedHtml5Features.File)
        {
            fe.AdviseChange(
                delegate ()
                {
                    ProcessFiles(fe.Files);
                });

            HtmlDocument.Current.Body.AdviseDrop(Dummies.Action<HtmlMouseEvent>, Dummies.Action<HtmlDropEvent>);
            InitDropArea("left");
            InitDropArea("right");
        }
        else
        {
            ShowInfo("Browser does not support file list");
        }
    }

    public static void Start()
    {
        new FileAPIApp();
    }

    void ShowInfo(string text)
    {
        try
        {
            if (SupportedHtml5Features.ArrayBuffer)
            {
                ArrayBuffer arrayBuffer = new ArrayBuffer(text.Length);
                DataView dataView = new DataView(arrayBuffer, 0, arrayBuffer.ByteLength);
                for (int i = 0; i < text.Length; i++)
                {
                    dataView.SetByte(i, (byte)text[i]);
                }

                if (SupportedHtml5Features.BlobBuilder)
                {
                    BlobBuilder blobBuilder = new BlobBuilder() { Type = "text/plain" };
                    Blob blob = blobBuilder
                        .Append(arrayBuffer)
                        .Append(text)
                        .CreateBlob();

                    string blobUrl = HtmlWindow.Current.CreateObjectUrl(blob);
                    if (blobUrl == null)
                    {
                        text += "\r\nCreateObjectUrl is not supported";
                    }
                    else
                    {
                        text += "\r\nBlobUrl " + blobUrl;
                        HtmlWindow.Current.RevokeObjectUrl(blobUrl);
                    }
                }
                else
                {
                    text += "\r\nBlobBuilder is not supported";
                }
            }
            else
            {
                text += "\r\nArrayBuffer is not supported";
            }
        }
        catch (Exception e)
        {
            text += "\r\n" + e.ExtractAllMessages();
        }

        info.InnerText = text;
    }

    void InitDropArea(string id)
    {
        HtmlElement drop = HtmlDocument.Current.GetElementById(id);
        HtmlStyle style = drop.Style;
        drop.AdviseDrop(
            delegate (HtmlMouseEvent e)
            {
                style.BorderColor = e != null ? "red" : "black";
            },
            delegate (HtmlDropEvent e)
            {
                style.BorderColor = "black";
                if (e == null)
                {
                    ShowInfo("Not dropped");
                }
                else if (e.Files.Length != 0)
                {
                    ProcessFiles(e.Files);
                }
                else
                {
                    ShowInfo("Dropped");
                }
            });
    }

    const int sliceSize = 1024 * 1024;

    void ProcessFiles(IList<File> files)
    {
        if (files.Count == 0)
        {
            ShowInfo("No files have been selected.");
            return;
        }

        File file = files[0];
        string name = file.Name;
        string type = file.Type;

        if (file.Size > sliceSize * 10)
        {
            long sourceSize = file.Size;
            Blob target = Copy(file);
            long copiedSize = target.Size;
            return;
        }
        string size = SupportedHtml5Features.BlobSize ? file.Size.ToString("N0") : "not supported";

        string chunckedSize = "Blob.Slice is not supported";
        if (SupportedHtml5Features.BlobSlice)
        {
            Blob blob = file.Slice(20);
            chunckedSize = (20 + blob.Size).ToString("N0");
        }

        string fileUrl = HtmlWindow.Current.CreateObjectUrl(file);
        HtmlWindow.Current.RevokeObjectUrl(fileUrl);
        string fileInfo = string.Format("Name {0}\r\n"
            + "Type {1}\r\n"
            + "Size {2}\r\n"
            + "Url {3}\r\n"
            + "Length {4:N0}\r\n"
            + "ChunckedSize {5}\r\n"
            ,
            name, type, size, fileUrl, files.Count, chunckedSize);

        if (SupportedHtml5Features.BlobRead)
        {
            file.ReadAsArrayBuffer(
                delegate (ArrayBuffer arrayBuffer)
                {
                    ByteArray ba = new ByteArray(arrayBuffer, 0, arrayBuffer.ByteLength);
                    ByteArray target = new ByteArray(20);
                    byte b = target[0];
                    b = target[10];
                    b = ba[0];
                    b = ba[10];
                    ba = ba.Slice(10, 18);
                    b = ba[0];
                    target.Set(ba, 2);
                    fileInfo = (string.Format("Readed {0:N0}\r\n", arrayBuffer.ByteLength)) + fileInfo;
                    Copy(file);
                    ShowInfo(fileInfo);
                },
                delegate (Exception e)
                {
                    ShowInfo(string.Format("Error {0}\r\n{1}", e, fileInfo));
                });
        }
        else
        {
            ShowInfo("Reading from blob is not supported\r\n" + fileInfo);
        }
    }

    Blob Copy(Blob source)
    {
        Blob target = null;
        while (source.Size >= sliceSize)
        {
            Blob slice = source.Slice(0, sliceSize);
            source = source.Slice(sliceSize);
            if (target == null)
            {
                target = slice;
            }
            else
            {
                target = new BlobBuilder()
                    .Append(target)
                    .Append(slice)
                    .CreateBlob();
            }
        }

        if (source.Size != 0)
        {
            if (target == null)
            {
                target = source;
            }
            else
            {
                target = new BlobBuilder()
                    .Append(target)
                    .Append(source)
                    .CreateBlob();
            }
        }

        return target;
    }
}

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta http-equiv="x-ua-compatible" content="IE=11;IE=10;IE=9;IE=8;IE=7" />
    <title></title>
    <script type="text/javascript" src="js/FileAPI.js"></script>
</head>
<body onload="Start()">
    <table>
        <tr>
            <td style="padding:4px">
                <input type="file" id="file" multiple />
            </td>
            <td style="padding:4px">
                <div style="width:60px;height:60px;border:1px solid black" id="left">
                    Drop files into the area
                </div>

            </td>
            <td style="padding:4px">
                <div style="width:60px;height:60px;border:1px solid black" id="right">
                    Drop files into the area
                </div>
            </td>
        </tr>
        <tr>
            <td>
                Info
            </td>
            <td id="info" colspan="2"></td>
        </tr>
    </table>
</body>
</html>
© 2009 JScriptSuite. All rights reserved.