It's all about the answers!

Ask a question

Upload attachment to the work item using server api.

Anurag Patel (21366) | asked Oct 11 '17, 6:56 a.m.
edited Oct 11 '17, 6:59 a.m.

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");
fis = new FileInputStream(attachmentFile);
IContentService contentService = getService(IContentService.class); 
IAttachment attachment = workItemServer.createAttachment(projectArea, monitor);
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 = (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) {
if (fis != null) {

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

permanent link
Ralph Schoon (63.2k33646) | answered Oct 11 '17, 8:02 a.m.

 I know that this code has worked for me:

Please note, there is no one who can remote debug your code.

Your answer

Register or to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.