You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wicket.apache.org by GitBox <gi...@apache.org> on 2021/04/08 08:02:24 UTC

[GitHub] [wicket] reiern70 commented on a change in pull request #466: add an AJAX behavior to transfer client side file information to ser…

reiern70 commented on a change in pull request #466:
URL: https://github.com/apache/wicket/pull/466#discussion_r609425400



##########
File path: wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java
##########
@@ -47,6 +56,73 @@
 {
 	private static final long serialVersionUID = 1L;
 
+	/**
+	 *  {@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 (reiern70@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();
+			String fileName = request.getRequestParameters().getParameterValue("fileName").toString();
+			Long fileSize = request.getRequestParameters().getParameterValue("fileSize").toLong(0);
+			Date lastModified = new Date(request.getRequestParameters().getParameterValue("lastModified").toLong(0));
+			String mimeType = request.getRequestParameters().getParameterValue("type").toString();
+			onFileSelected(target, fileName, fileSize, lastModified, mimeType);
+		}
+
+		/**
+		 * Called when a file, at client side is selected.
+		 *
+		 * @param target The {@link org.apache.wicket.ajax.AjaxRequestTarget}
+		 * @param fileName The client file name
+		 * @param fileSize The client file size
+		 * @param lastModified The {@link java.util.Date} when file was last modified.
+		 * @param mimeType The  MIME type of file.
+		 */
+		protected abstract void onFileSelected(AjaxRequestTarget target, String fileName, Long fileSize,
+											   Date lastModified, String mimeType);
+
+		@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()+"_file = this.files[0]; return true; } " +
+							"else return false;";
+				}
+			});
+
+			String var = "window."+getComponent().getMarkupId()+"_file";

Review comment:
       ![image](https://user-images.githubusercontent.com/462655/113990145-29f0ff00-981f-11eb-8503-0608ff407204.png)
   
   yes. 




-- 
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:
users@infra.apache.org