How to upload multiple Attachments with dropzone to Confluence?
JavaScript
$("#upload").dropzone({ url: '/rest/dropzone/1.0/upload/' + config.pageid, previewTemplate: '<div class="dz-preview dz-file-preview dz-processing dz-success dz-complete"><div class="dz-image"><img data-dz-thumbnail=""></div><div class="dz-details"><div class="dz-size"><span data-dz-size=""></div><div class="dz-filename"><span data-dz-name="">portal.iml</span></div></div><div class="dz-error-message"><span data-dz-errormessage=""></span></div></div>' });
Plugin REST Service
@POST @Path("/upload/{id}") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response handleFileUpload(@PathParam("id") final Long id, @MultipartFormParam("file") Collection<FilePart> fileParts) { try { for (FilePart filePart : fileParts) { InputStream inputstreamByFile = getInputstreamByFile(filePart); File fileFromFilePart = getFileFromFilePart(filePart); InputStream is = new FileInputStream(fileFromFilePart); Attachment att = new Attachment(); this.attachmentManager.saveAttachment(att, null, is); Page page = this.pageManager.getPage(id); page.addAttachment(att); return Response.ok(att).build(); } } catch (IOException e) { log.error("handleFileUpload IOException: " + e.getMessage(), e); } return Response.ok("{\"status\":\"error\"}").build(); }
I always get following ERROR:
ERROR [http-bio-8090-exec-307] [common.error.jersey.ThrowableExceptionMapper] toResponse Uncaught exception thrown by REST service: Attachment content
-- referer: http://... url: /rest/dropzone/1.0/upload/21889030 | userName: admin
java.lang.IllegalArgumentException: Attachment content
at org.springframework.util.Assert.notNull(Assert.java:112)
at com.atlassian.confluence.pages.DefaultAttachmentManager.saveAttachments(DefaultAttachmentManager.java:144)
at com.atlassian.confluence.pages.DefaultAttachmentManager.saveAttachment(DefaultAttachmentManager.java:128)
at com.atlassian.confluence.pages.DelegatorAttachmentManager.saveAttachment(DelegatorAttachmentManager.java:136)
at com.atlassian.confluence.pages.CachingAttachmentManager.saveAttachment(CachingAttachmentManager.java:178)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Community moderators have prevented the ability to post new answers.
You can not save attachment created with empty constructor. At least few fields have to be set. Here is snapshot that works for me.
// hardcoded page id. Attachment must belong to some page. Long pageId = 98334L; Page page = (Page) contentEntityManager.getById(pageId); try { Attachment attachment = new Attachment(file.getName(), "application/x-upload-data", file.length(), ""); attachment.setCreator(user); attachment.setCreationDate(new Date()); attachment.setLastModificationDate(new Date()); page.addAttachment(attachment); // This is needed! attachmentManager.saveAttachment(attachment, null, new FileInputStream(file)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Thanks. This was the right Tip: page.addAttachment(attachment); // This is needed! attachmentManager.saveAttachment(attachment, null, new FileInputStream(file)); My complete Method: @POST @Path("/upload/{id}") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response handleFileUpload(@PathParam("id") final Long id, @MultipartFormParam("file") Collection<FilePart> fileParts) throws IOException { ConfluenceUser user = get(); if(user != null) { try { for (FilePart filePart : fileParts) { File file = getFileFromFilePart(filePart); Page page = this.pageManager.getPage(id); Attachment attachment = new Attachment(file.getName(), "application/x-upload-data", file.length(), ""); attachment.setCreator(user); attachment.setCreationDate(new Date()); attachment.setLastModificationDate(new Date()); page.addAttachment(attachment); this.attachmentManager.saveAttachment(attachment, null, new FileInputStream(file)); return Response.ok(attachment).build(); } } catch (IOException e) { log.error("CommentRestResource handleFileUpload IOException: " + e.getMessage(), e); } catch (AttachmentDataStreamSizeMismatchException e) { log.error("AttachmentDataStreamSizeMismatchException handleFileUpload IOException: " + e.getMessage(), e); } } return Response.ok("{\"status\":\"error\"}").build(); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Step 1: Step 1: Throws: java.io.IOException java.lang.IllegalArgumentException - if attachment's content is not set Missing: att.setContainer(page); Result: ERROR [http-bio-8090-exec-322] [common.error.jersey.ThrowableExceptionMapper] toResponse Uncaught exception thrown by REST service: Attachment data stream contains a different number of bytes to the declared size of the attachment. Expected: 0, actual: 89516 ... com.atlassian.confluence.pages.attachments.AttachmentDataStreamSizeMismatchException: Attachment data stream contains a different number of bytes to the declared size of the attachment. Expected: 0, actual: 89516
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.