diff --git a/Ooui/Form.cs b/Ooui/Form.cs index 557a72b..e23e22a 100644 --- a/Ooui/Form.cs +++ b/Ooui/Form.cs @@ -10,7 +10,19 @@ namespace Ooui set => SetProperty (ref action, value ?? "", "action"); } - public event TargetEventHandler Submitted { + string method = "GET"; + public string Method { + get => method; + set => SetProperty (ref method, value ?? "", "method"); + } + + string enctype = "application/x-www-form-urlencoded"; + public string EncodingType { + get => enctype; + set => SetProperty (ref enctype, value ?? "", "enctype"); + } + + public event TargetEventHandler Submitted { add => AddEventListener ("submit", value); remove => RemoveEventListener ("submit", value); } diff --git a/Ooui/UI.cs b/Ooui/UI.cs index db72c68..deb7901 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -122,6 +122,11 @@ namespace Ooui Publish (path, new DataHandler (data, JsonHandler.ContentType)); } + public static void PublishCustomResponse (string path, Action responder) + { + Publish (path, new CustomHandler (responder)); + } + static string GuessContentType (string path, string filePath) { return null; @@ -361,6 +366,21 @@ namespace Ooui } } + class CustomHandler : RequestHandler + { + readonly Action responder; + + public CustomHandler (Action responder) + { + this.responder = responder; + } + + public override void Respond (HttpListenerContext listenerContext, CancellationToken token) + { + responder (listenerContext, token); + } + } + static async void ProcessWebSocketRequest (HttpListenerContext listenerContext, CancellationToken serverToken) { // diff --git a/Samples/FilesSample.cs b/Samples/FilesSample.cs new file mode 100644 index 0000000..626cbb0 --- /dev/null +++ b/Samples/FilesSample.cs @@ -0,0 +1,45 @@ +using System; +using System.Net; +using System.Threading; +using Ooui; + +namespace Samples +{ + public class FilesSample + { + public void Publish () + { + var heading = new Heading ("Upload Files"); + var subtitle = new Paragraph ("Upload files to the app"); + + var uploadForm = new Form (); + uploadForm.Action = "/files/upload"; + uploadForm.Method = "POST"; + uploadForm.EncodingType = "multipart/form-data"; + uploadForm.AppendChild (new Input (InputType.File) { Name = "file" }); + uploadForm.AppendChild (new Input (InputType.Submit) { Value = "Upload" }); + + var app = new Div (); + app.AppendChild (heading); + app.AppendChild (subtitle); + app.AppendChild (uploadForm); + + UI.Publish ("/files", app); + + UI.PublishCustomResponse ("/files/upload", HandleUpload); + } + + void HandleUpload (HttpListenerContext listenerContext, CancellationToken token) + { + var req = listenerContext.Request; + var ct = req.ContentType; + var bi = ct.IndexOf ("boundary=", StringComparison.InvariantCulture); + var boundaryString = ct.Substring (bi + 9); + var boundaryBytes = System.Text.Encoding.UTF8.GetBytes (boundaryString); + var boundaryEndBytes = System.Text.Encoding.UTF8.GetBytes (boundaryString + "--"); + Console.WriteLine ("OMGGGGGG " + boundaryString); + listenerContext.Response.StatusCode = 200; + listenerContext.Response.Close (); + } + } +} diff --git a/Samples/Program.cs b/Samples/Program.cs index 251adf7..72fd445 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -26,8 +26,9 @@ namespace Samples new ButtonSample ().Publish (); new TodoSample ().Publish (); new DrawSample ().Publish (); + new FilesSample ().Publish (); - UI.Present ("/draw"); + UI.Present ("/files"); Console.ReadLine (); }