public UploadableProfileImagePanel(String id, final long userId) { super(id, userId); final Form<Void> form = new Form<Void>("form"); form.setMultiPart(true); form.setMaxSize(Bytes.bytes(getBean(ConfigurationDao.class).getMaxUploadSize())); // Model is necessary here to avoid writing image to the User object form.add( fileUploadField = new FileUploadField( "image", new IModel<List<FileUpload>>() { private static final long serialVersionUID = 1L; // FIXME this need to be eliminated public void detach() {} public void setObject(List<FileUpload> object) {} public List<FileUpload> getObject() { return null; } })); form.add(new UploadProgressBar("progress", form, fileUploadField)); fileUploadField.add( new AjaxFormSubmitBehavior(form, "onchange") { private static final long serialVersionUID = 2160216679027859231L; @Override protected void onSubmit(AjaxRequestTarget target) { FileUpload fu = fileUploadField.getFileUpload(); if (fu != null) { StoredFile sf = new StoredFile(fu.getClientFileName()); if (sf.isImage()) { boolean asIs = sf.isAsIs(); try { // FIXME need to work with InputStream !!! getBean(GenerateImage.class) .convertImageUserProfile(fu.writeToTempFile(), userId, asIs); } catch (Exception e) { // TODO display error e.printStackTrace(); } } else { // TODO display error } } target.add(profile, form); } }); add(form.setOutputMarkupId(true)); add(BootstrapFileUploadBehavior.INSTANCE); }
public ImageUploadContentPanel(String pId, String customUploadFolderPath) { super(pId); setOutputMarkupId(true); this.uploadFolderPath = customUploadFolderPath; Form<?> form = new Form<Void>("form"); final FeedbackPanel feedback = new FeedbackPanel("feedback"); feedback.setOutputMarkupId(true); form.add(feedback); final FileUploadField fileUploadField = new FileUploadField("file"); fileUploadField.setLabel(new ResourceModel("required.label")); fileUploadField.setRequired(true); fileUploadField.add(FILE_EXTENSION_VALIDATOR); form.add(fileUploadField); form.add( new AjaxButton("uploadButton", form) { private static final long serialVersionUID = 1L; @Override protected void onSubmit(AjaxRequestTarget pTarget, Form<?> pForm) { FileUpload fileUpload = fileUploadField.getFileUpload(); String fileName = fileUpload.getClientFileName(); try { File currentEngineerDir = new File(uploadFolderPath); if (!currentEngineerDir.exists()) { currentEngineerDir.mkdir(); } fileUpload.writeTo(new File(currentEngineerDir, fileName)); } catch (Exception ex) { log.error("Can't upload attachment: " + ex.getMessage(), ex); ImageUploadContentPanel.this.error("Can't upload attachment"); pTarget.add(feedback); return; } finally { fileUpload.closeStreams(); } ImageFileDescription imageFileDescription = new ImageFileDescription(fileName); imageFileDescription.setContentType(fileUpload.getContentType()); onImageUploaded(imageFileDescription, pTarget); } @Override protected void onError(AjaxRequestTarget pTarget, Form<?> pForm) { pTarget.add(feedback); } }); add(form); }