Dear all, I had a requirement for a FileUploader and found this https://github.com/valums/file-uploader
Integration with Tapestry5 was as usual very easy... Am sharing the code, you might find it useful package somepackage.components; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.tapestry5.BindingConstants; import org.apache.tapestry5.ClientElement; import org.apache.tapestry5.ComponentResources; import org.apache.tapestry5.Link; import org.apache.tapestry5.MarkupWriter; import org.apache.tapestry5.annotations.Import; import org.apache.tapestry5.annotations.Parameter; import org.apache.tapestry5.annotations.SupportsInformalParameters; import org.apache.tapestry5.ioc.Messages; import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.json.JSONLiteral; import org.apache.tapestry5.json.JSONObject; import org.apache.tapestry5.services.Request; import org.apache.tapestry5.services.RequestGlobals; import org.apache.tapestry5.services.javascript.JavaScriptSupport; import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; @SupportsInformalParameters @Import(library = "fileuploader/fileuploader.js", stylesheet = "fileuploader/fileuploader.css") public class FileUploader implements ClientElement { @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL) private String clientId; @Inject private JavaScriptSupport javaScriptSupport; @Inject private ComponentResources resources; @Parameter(required = true, allowNull = false) private List<String> filenames; @Parameter private String tempDir; @Parameter private String clientFileName; @SuppressWarnings("unused") @Parameter private boolean uploaded; @Parameter(value = "2000000", defaultPrefix = BindingConstants.LITERAL) private int maxSize; @Parameter(value = "0", defaultPrefix = BindingConstants.LITERAL) private int minSize; @Parameter(value = "1", defaultPrefix = BindingConstants.LITERAL) private int maxFiles; @Inject private Request request; @Inject private Messages messages; @Inject private RequestGlobals globals; private String assignedClientId; @Parameter(value = "tmp", allowNull = false, defaultPrefix = BindingConstants.LITERAL) private String prefix; public void beginRender() { assignedClientId = javaScriptSupport.allocateClientId(clientId); } public void afterRender(final MarkupWriter writer) { writer.element("div", "id", getClientId()); writer.end(); final Link link = resources.createEventLink("upload"); JSONObject spec = new JSONObject(); spec.put("element", new JSONLiteral("document.getElementById('" + getClientId() + "')")); spec.put("action", link.toAbsoluteURI()); spec.put("sizeLimit", maxSize); spec.put("minSizeLimit", minSize); for (String informalParameter : resources.getInformalParameterNames()) { spec.put(informalParameter, resources.getInformalParameter( informalParameter, String.class)); } javaScriptSupport.addScript("new qq.FileUploader(%s);", spec); } Object onUpload() { uploaded = false; String errorMessage = null; clientFileName = request.getParameter("qqfile"); String suffix = ".tmp"; if(clientFileName.lastIndexOf('.') != -1){ suffix = clientFileName.substring(0, clientFileName.lastIndexOf('.') - 1); } if(maxFiles <= filenames.size()){ JSONObject spec = new JSONObject(); spec.put("success", false).put("error", messages.format("fileuploader.maxfiles", maxFiles)); return spec; } try { InputStream in = globals.getHTTPServletRequest().getInputStream(); ByteOutputStream bout = new ByteOutputStream(); byte[] buf = new byte[8096]; while (true) { int bytesRead = in.read(buf); if (bytesRead == -1) { break; } bout.write(buf, 0, bytesRead); } bout.close(); if (bout.getCount() < minSize) { errorMessage = messages.get("fileupload.minsize"); } else if (bout.getCount() > maxSize) { errorMessage = messages.get("fileupload.maxsize"); } else { FileOutputStream fout = null; try { File dir = null; if(tempDir != null){ dir = new File(tempDir); dir.mkdir(); } File tempFile = File.createTempFile(prefix, suffix, dir); fout = new FileOutputStream(tempFile); fout.write(bout.getBytes(), 0, bout.getCount()); fout.close(); uploaded = true; filenames.add(tempFile.getAbsolutePath()); } catch (FileNotFoundException e) { errorMessage = e.getMessage(); } catch (IOException e) { errorMessage = e.getMessage(); } finally { if (fout != null) { fout.close(); } } } } catch (IOException e) { errorMessage = messages.get("fileupload.writeerror"); } JSONObject result = new JSONObject(); if (errorMessage != null) { result.put("success", false); result.put("error", errorMessage); } else { result.put("success", true); } return result; } public String getClientId() { return assignedClientId; } } regards Taha