11{
12 private const string savePath = "./uploaded";
13
14 public override void ProcessGet()
15 {
16 if (req.
URL ==
"/fileupload.html")
17 {
18 res.
SendHTMLContent(
"<form action=\"/fileupload\" method=\"post\" enctype=\"multipart/form-data\">" +
19 "<input type=\"text\" name=\"value1\" id=\"value1\"></input>" +
20 "<input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\">" +
21 "<input type=\"submit\" value=\"Upload\" name=\"submit\">" +
22 "</form>");
23 }
24 else if (req.
URL ==
"/fileupload")
25 {
27 }
28 else if (req.
URL ==
"/fileuploadmulti.html")
29 {
30 res.
SendHTMLContent(
"<form action=\"/fileupload\" method=\"post\" enctype=\"multipart/form-data\">" +
31 "<input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\">" +
32 "<input type=\"file\" name=\"fileToUpload2\" id=\"fileToUpload2\">" +
33 "<input type=\"submit\" value=\"Upload\" name=\"submit\">" +
34 "</form>");
35 }
36 else
37 {
39 }
40 }
41
42 public override void ProcessPost()
43 {
45 {
47 if (data != null)
48 {
49 var files = data.GetFiles();
50 if (files.Count < 0)
51 {
52 res.
SendCode(HTTP_CODES.NOT_ACCEPTABLE);
53 return;
54 }
55 if (!Path.Exists(savePath))
56 {
57 Directory.CreateDirectory(savePath);
58 foreach (var f in files)
59 {
60 f.SaveToDisk(savePath);
61 Terminal.INFO(f);
62 }
63 }
65 return;
66 }
67 res.
SendCode(HTTP_CODES.NOT_ACCEPTABLE);
68 }
69 else
70 {
72 }
73
74 }
75}
bool IsFileUpload()
Returns true if the request is a file upload.
MultiPartFormData? GetMultiPartFormData()
Returns the form data if the request is a multipart formdata upload, else null.
string URL
Return the url of the request.
void SendCode(int statusCode)
Send an HTTP Response with no body but with given status code.
void SendHTMLContent(string content, bool process=false, int statusCode=HTTP_CODES.OK, string encoding="UTF-8", Dictionary< string, string >? customHeaders=null)
Sends an html page passed as string.
void SendFile(string absPath, string? mimeType=null, int statusCode=HTTP_CODES.OK, Dictionary< string, string >? customHeaders=null)
Loads a file from a given path and sends an HTTP Response.
class MultiPartFormData(byte[] body, string boundary)