public static Task<byte[]> readAsBytes(this File f)
        {
            // X:\jsc.svn\examples\javascript\io\WebApplicationSelectingFile\WebApplicationSelectingFile\Application.cs
            // X:\jsc.svn\examples\javascript\io\DropFileForMD5Experiment\DropFileForMD5Experiment\Application.cs



            var y = new TaskCompletionSource<byte[]>();

            var x = new FileReader();
            //Console.WriteLine("readAsText FileReader");
            x.onload =
                new Action(
                    delegate
                    {
                        var a = (ArrayBuffer)x.result;

                        var u8c = new Uint8ClampedArray(array: a);

                        // X:\jsc.svn\core\ScriptCoreLib\JavaScript\BCLImplementation\System\Net\WebClient.cs

                        y.SetResult((byte[])u8c);
                    }
                );


            // partial build?
            // move to .Async?

            x.readAsArrayBuffer(f);
            //Console.WriteLine("readAsText FileReader readAsText");

            return y.Task;
        }
Exemplo n.º 2
0
        public static void ReadAllText(this global::System.IO.FileInfo f, Action<string> yield)
        {
            var ff = (__FileInfo)(object)f;

            var r = new FileReader();

            r.onload = IFunction.OfDelegate(
                new Action(
                    delegate
                    {
                        yield((string)r.result);
                    }
                )
            );

            r.readAsText(ff.InternalFile, null);

        }
Exemplo n.º 3
0
        /// <summary>
        /// This is a javascript application.
        /// </summary>
        /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param>
        public Application(IDefault  page)
        {
            #region AtFile
            Action<File> AtFile =
                f =>
                {
                    var n = new MyFile();
                    // databind here?
                    n.name.innerText = f.name;
                    n.type.innerText = f.type;
                    n.lastModifiedDate.innerText = "" + f.lastModifiedDate;

                    #region hex
                    new FileReader().With(
                        reader =>
                        {
                            reader.onload = IFunction.Of(
                                delegate
                                {
                                    var x = (ArrayBuffer)reader.result;

                                    var u8 = new Uint8Array(x, 0, 8);

                                    var pre = new IHTMLPre().AttachTo(n.Container);

                                    pre.style.border = "1px solid gray";

                                    for (uint i = 0; i < u8.length; i++)
                                    {
                                        pre.innerText += u8[i].ToString("x2") + " ";

                                    }
                                }
                            );

                            // Read in the image file as a data URL.
                            reader.readAsArrayBuffer(f);
                        }
                     );
                    #endregion

                    #region AsDataURL
                    Action<Action<string>> AsDataURL =
                        h =>
                        {
                            var reader = new FileReader();

                            reader.onload = IFunction.Of(
                                delegate
                                {
                                    var base64 = (string)reader.result;

                                    h(base64);

                                }
                            );
                            
                            // Read in the image file as a data URL.
                            reader.readAsDataURL(f);

                        };
                    #endregion


                    #region image
                    if (f.type.StartsWith("image/"))
                    {
                        AsDataURL(
                            base64 =>
                                new IHTMLImage
                                {
                                    src = base64,

                                }.AttachTo(n.Container)
                        );
                    }
                    #endregion


                    #region audio
                    if (f.type.StartsWith("audio/"))
                    {
                        AsDataURL(
                           base64 =>
                               new IHTMLAudio
                               {
                                   src = base64,
                                   controls = true,
                               }.AttachTo(n.Container)
                       );
                    }
                    #endregion

                    #region video
                    //if (f.type.StartsWith("video/"))
                    //{
                    //    AsDataURL(
                    //       base64 =>
                    //           new IHTMLVideo
                    //           {
                    //               src = base64,
                    //               controls = true,

                    //           }.AttachTo(n.Container)
                    //   );
                    //}
                    #endregion

                    n.Container.AttachTo(page.list);
                };
            #endregion


            #region onchange
            page.files.onchange +=
                e =>
                {
                    FileList x = page.files.files;
                    page.list.Clear();
                    page.list.Add("files: " + x.length);
                    for (uint i = 0; i < x.length; i++)
                    {
                        File f = x[i];

                        AtFile(f);
                    }

                };
            #endregion

            page.drop_zone.ondragleave +=
                delegate
                {
                    page.drop_zone.style.borderColor = JSColor.Gray;
                    page.drop_zone.style.backgroundColor = JSColor.None;
                };

            #region dragover
            page.drop_zone.ondragover +=
                    evt =>
                    {
                        page.drop_zone.style.borderColor = JSColor.Red;
                        page.drop_zone.style.backgroundColor = JSColor.FromRGB(0xff, 0xaf, 0xaf);

                        evt.StopPropagation();
                        evt.PreventDefault();
                        evt.dataTransfer.dropEffect = "copy"; // Explicitly show this is a copy.
                    };

            page.drop_zone.ondrop +=
                  evt =>
                  {
                      page.drop_zone.style.borderColor = JSColor.Green;
                      page.drop_zone.style.backgroundColor = JSColor.FromRGB(0xaf, 0xff, 0xaf);


                      evt.StopPropagation();
                      evt.PreventDefault();

                      FileList x = evt.dataTransfer.files; // FileList object.

                      page.list.Clear();
                      page.list.Add("files: " + x.length);
                      for (uint i = 0; i < x.length; i++)
                      {
                          File f = x[i];

                          AtFile(f);
                      }
                  };
            #endregion


        }
Exemplo n.º 4
0
        public static void ToDataURLAsync(this Blob f, Action<string> y)
        {
            var reader = new FileReader();

            reader.onload = IFunction.Of(
                delegate
                {
                    var base64 = (string)reader.result;

                    y(base64);

                }
            );

            // Read in the image file as a data URL.
            reader.readAsDataURL(f);
        }
        public static Task<string> readAsText(this File f)
        {
            // tested by
            // X:\jsc.svn\examples\javascript\chrome\apps\ChromeCSVFileHandler\ChromeCSVFileHandler\Application.cs

            var y = new TaskCompletionSource<string>();

            var x = new FileReader();
            //Console.WriteLine("readAsText FileReader");
            x.onload =
                new Action(
                    delegate
                    {
                        //Console.WriteLine("readAsText FileReader onload");
                        y.SetResult((string)x.result);
                    }
                );


            x.readAsText(f, "UTF-8");
            //Console.WriteLine("readAsText FileReader readAsText");

            return y.Task;
        }