Created
November 1, 2013 02:26
-
-
Save mockey/7260194 to your computer and use it in GitHub Desktop.
multipart upload test for mod_neko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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