Working on files support

This commit is contained in:
Frank A. Krueger 2017-08-22 20:26:01 -07:00
parent a171703b07
commit 817454dea4
4 changed files with 80 additions and 2 deletions

View File

@ -10,6 +10,18 @@ namespace Ooui
set => SetProperty (ref action, value ?? "", "action"); set => SetProperty (ref action, value ?? "", "action");
} }
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 { public event TargetEventHandler Submitted {
add => AddEventListener ("submit", value); add => AddEventListener ("submit", value);
remove => RemoveEventListener ("submit", value); remove => RemoveEventListener ("submit", value);

View File

@ -122,6 +122,11 @@ namespace Ooui
Publish (path, new DataHandler (data, JsonHandler.ContentType)); Publish (path, new DataHandler (data, JsonHandler.ContentType));
} }
public static void PublishCustomResponse (string path, Action<HttpListenerContext, CancellationToken> responder)
{
Publish (path, new CustomHandler (responder));
}
static string GuessContentType (string path, string filePath) static string GuessContentType (string path, string filePath)
{ {
return null; return null;
@ -361,6 +366,21 @@ namespace Ooui
} }
} }
class CustomHandler : RequestHandler
{
readonly Action<HttpListenerContext, CancellationToken> responder;
public CustomHandler (Action<HttpListenerContext, CancellationToken> responder)
{
this.responder = responder;
}
public override void Respond (HttpListenerContext listenerContext, CancellationToken token)
{
responder (listenerContext, token);
}
}
static async void ProcessWebSocketRequest (HttpListenerContext listenerContext, CancellationToken serverToken) static async void ProcessWebSocketRequest (HttpListenerContext listenerContext, CancellationToken serverToken)
{ {
// //

45
Samples/FilesSample.cs Normal file
View File

@ -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 ();
}
}
}

View File

@ -26,8 +26,9 @@ namespace Samples
new ButtonSample ().Publish (); new ButtonSample ().Publish ();
new TodoSample ().Publish (); new TodoSample ().Publish ();
new DrawSample ().Publish (); new DrawSample ().Publish ();
new FilesSample ().Publish ();
UI.Present ("/draw"); UI.Present ("/files");
Console.ReadLine (); Console.ReadLine ();
} }