Skip to content

Instantly share code, notes, and snippets.

@mockey
Created November 1, 2013 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mockey/7260194 to your computer and use it in GitHub Desktop.
Save mockey/7260194 to your computer and use it in GitHub Desktop.
multipart upload test for mod_neko
package;
import haxe.io.Bytes;
import neko.Lib;
import neko.Web;
import sys.io.File;
import sys.io.FileOutput;
using StringTools;
class Main {
var isFileUpload:Bool;
var output:FileOutput;
static function main() {
new Main();
}
function new() {
if (Web.getMethod() == "POST") handlePost();
showPage();
}
function handlePost() {
if (Web.getClientHeader("Content-Type").startsWith("multipart/form-data")) {
Web.parseMultipart(onPart, onData);
if (isFileUpload) output.close();
}
}
function onPart(name:String, file:String) {
if (isFileUpload) output.close();
isFileUpload = file != null;
if (isFileUpload)
output = File.write(Web.getCwd() + file, true);
}
function onData(bytes:Bytes, pos:Int, len:Int) {
if (len > 0)
output.writeBytes(bytes, pos, len);
}
function showPage() {
Lib.print('<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Test</h1>
<form action="" method="post" enctype="multipart/form-data">
<input name="file" type="file" /><br />
<input type="submit" value="submit" />
</form>
</body>
</html>
');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment