reiern70 commented on a change in pull request #467: URL: https://github.com/apache/wicket/pull/467#discussion_r610423763
########## File path: wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java ########## @@ -47,6 +59,146 @@ { private static final long serialVersionUID = 1L; + /** + * Description of file properties as in browser client side. + */ + public static class FileDescription implements Serializable { + + private static final long serialVersionUID = 1L; + + private final String fileName; + private final long fileSize; + private final Date lastModified; + private final String mimeType; + + public FileDescription(String fileName, long fileSize, long lastModified, String mimeType) { + this.fileName = fileName; + this.fileSize = fileSize; + this.lastModified = new Date(lastModified); + this.mimeType = mimeType; + } + + public String getFileName() { + return fileName; + } + + public long getFileSize() { + return fileSize; + } + + public Date getLastModified() { + return lastModified; + } + + public String getMimeType() { + return mimeType; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + FileDescription that = (FileDescription) o; + return fileName.equals(that.fileName); + } + + @Override + public int hashCode() { + return Objects.hash(fileName); + } + } + + /** + * {@link org.apache.wicket.ajax.form.OnChangeAjaxBehavior} that streams back to server properties + * of the selected file (at client side), even when file has not yet being uploaded. +s * + * @author Ernesto Reinaldo Barreiro (reier...@gmail.com). + */ + public static abstract class OnFileSelectedBehavior extends OnChangeAjaxBehavior + { + private static final long serialVersionUID = 1L; + + @Override + protected void onBind() { + super.onBind(); + Component component = getComponent(); + if (!(component instanceof FileUploadField)) + { + throw new WicketRuntimeException("Behavior " + getClass().getName() + + " can only be added to an instance of a FileUploadField"); + } + } + + @Override + protected void onUpdate(AjaxRequestTarget target) + { + Request request = RequestCycle.get().getRequest(); + List<FileDescription> fileDescriptions = new ArrayList<>(); + IRequestParameters parameters = request.getRequestParameters(); + String[] fileNames = parse(parameters.getParameterValue("fileName")); + String[] fileSizes = parse(parameters.getParameterValue("fileSize")); + String[] lastModifiedDates = parse(parameters.getParameterValue("lastModified")); + String[] mimeTypes = parse(parameters.getParameterValue("mimeType")); + for (int i = 0; i < fileNames.length; i++) { + FileDescription fileDescription = new FileDescription( + fileNames[i], + Long.parseLong(fileSizes[i]), + Long.parseLong(lastModifiedDates[i]), + mimeTypes[i]); + fileDescriptions.add(fileDescription); + } + onFileSelected(target, fileDescriptions); + } + + private String[] parse(StringValue stringValue) { + return stringValue.toString().split(","); + } + + /** + * Called when a file, at client side is selected. + * + * @param target The {@link org.apache.wicket.ajax.AjaxRequestTarget} + * @param fileDescriptions A list of FileDescription + */ + protected abstract void onFileSelected(AjaxRequestTarget target, List<FileDescription> fileDescriptions); + + @Override + protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { + super.updateAjaxAttributes(attributes); + attributes.getAjaxCallListeners().add(new IAjaxCallListener() + { + @Override + public CharSequence getPrecondition(Component component) + { + return "if (this.files) { window." + component.getMarkupId() + "_files = this.files; return true; } " + Review comment: Also I though about cleaning garbage after use it: this only needs to be kept in context of this AJAX request. Maybe we can even use attrs object as a holder. I will check. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org