Working on files support
This commit is contained in:
parent
a171703b07
commit
817454dea4
14
Ooui/Form.cs
14
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);
|
||||
}
|
||||
|
|
20
Ooui/UI.cs
20
Ooui/UI.cs
|
@ -122,6 +122,11 @@ namespace Ooui
|
|||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
//
|
||||
|
|
|
@ -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 ();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 ();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue