webapp cat; import Binary; import Webapp; import HTMLDocument; import IO; data WebReturn = Form(HTMLDocument doc) | FileData(Binary file, [(String,String)] ctype); // we might output one of two types of data Int maxsize = 20480; // 20k Void webconfig { allowFileUploads(); setKayaUploadDir("/users/kaya/share/tmp"); // change this to one suitable for you setKayaMaxPost(maxsize); } WebReturn webmain() { return runHandler(@makeForm); } WebReturn makeForm() { doc = HTMLDocument::new(HTML4Strict,"Webcat"); void(addHeading(doc.body,1,"Webcat")); form = addLocalForm(doc.body,true); // note second parameter to addLocalForm to enable file uploads on this form. f1 = addFieldset(form,"'cat' a file"); void(addLabelledInput(f1,"Select file",InputFile,"file","")); void(addLabelledTextarea(f1,"extratext","Send extra text (ignored)")); if (incomingExists("extratext",DataPost)) { void(addPreformatted(f1,incomingValue("extratext",DataPost))); } void(addLocalControlInput(f1,"Upload file",catFile@(),true)); return Form(doc); } WebReturn catFile(Bool dummy) { // don't need a parameter here if (!incomingFileExists("file")) { // no file uploaded, so redisplay the form return makeForm(); } file = incomingFile("file"); ctype = contentType(file); fname = originalName(file); // get information about the file temp = tempPath(file); // find the temporary filename fh = open(temp,[Read]); bin = readBlock(fh,maxsize); // can't be bigger than maxsize. // if it mattered more, of course, we could check how big the // file was before trying to read it. close(fh); // and close the file handle. Now we construct our return value headers = [("Content-type",ctype), ("Content-disposition","attachment; filename="+fname)]; // these headers will serve the file back to the browser with the content // type it gave, and if it supports Content-disposition, a suggestion that // it use the name it gave us for it. return FileData(bin,headers); } Void displayPage(WebReturn output) { // finally we need to write our own displayPage function for our // custom data type case output of { Form(doc) -> displayPage(doc); | FileData(bin,headers) -> displayPage(bin,headers); } // in both cases we use the built-in Webapp handler for either HTML or // Binary data }