Upload attachment to the work item using server api.
Hi All,
I had written a simple code to attach a text file and excel file to the work item using server api.
For this I written follow up action and trigger the action on save operation of work item.
File has been uploaded and link to the work item successfuly but extension is different.
If I download the uploaded file from work item then file extension is of type : 'File'.
Below is the code :
--------------------------
FileInputStream fis= null;
File attachmentFile = new File("D:\serverInfo\sheet1.xlsx");
try{
fis = new FileInputStream(attachmentFile);
IContentService contentService = getService(IContentService.class);
IAttachment attachment = workItemServer.createAttachment(projectArea, monitor);
attachment.setName("abc");
attachment.setDescription("test");
IContent content = contentService.storeContent3(IContent.CONTENT_TYPE_UNKNOWN, IContent.ENCODING_UTF_8,LineDelimiter.LINE_DELIMITER_NONE,new FileInputStream(attachmentFile), attachmentFile.length(), null, HashCode.createHashCode(new FileInputStream(attachmentFile)));
attachment.setContent(content);
attachment = (IAttachment) attachment.getWorkingCopy();
workItemServer.saveAttachment(attachment, monitor);
IItemReference reference = WorkItemLinkTypes.createAttachmentReference(attachment);
IWorkItemReferences targetReferences = workItemServer.resolveWorkItemReferences(rcn, null);
targetReferences.add(WorkItemEndPoints.ATTACHMENT, reference);
saveWorkitem(rcn, saveParameter, workItemServer,targetReferences);
System.out.println("Create attachment stop");
}catch (Exception e) {
e.printStackTrace();
}finally{
if (fis != null) {
fis.close();
}
}
Please suggest the changes needed in this code so that if I will upload the file using this code and download the file then file extension should be the same while upload the file.
Thanks in Advance.
One answer
I know that this code has worked for me: https://rsjazz.wordpress.com/2012/08/01/uploading-attachments-to-work-items/
Please note, there is no one who can remote debug your code.