Not able to download file using cam-file-download when file is set from Task Listener

I am trying to read a file from a filestore location and set it in the DelegateExecution from within a Task Listener with the intention to be able to download it from the succeeding form. But I do not get the download link on the next html form.

The process is defined as below. After Start Event, Task A is called. On Task A I upload a file attached to variable infoDoc. Code in html as below

<input type="file" cam-variable-name="infoDoc" cam-variable-type="File" cam-max-filesize="10000000" />

On Completion of Task A it’s Task Listener is called. Inside it I programmatically upload a file to DelegateExecution as below.

FileValue typedFileValue = Variables.fileValue("docpack.zip") .file(new File("C:\tmp\files\docs-pack.zip")) .mimeType("application/zip").encoding("UTF-8").create();

delegateExecution.setVariableLocal("DOCSPACK", typedFileValue);`

Alternatively also tried below code to set file variable. Both are not working

delegateExecution.getProcessEngineServices().getRuntimeService().setVariable(delegateExecution.getId(),"DOCSPACK", typedFileValue);

On Task B form I can download file attached to variable infoDoc. Code below

<a cam-file-download=“infoDoc”></a>

But I cannot see any link to download file attached to variable DOCSPACK. It just comes out blank.

I checked the ACT_RU_VARIABLE table and noticed a few differences between variables infoDoc and DOCSPACK

For variable NAME = infoDoc

  TYPE_ = file
  REV_ = 1
  TEXT_ = test.txt
  TEXT2_ = text/plain#
  SEQUENCE_COUNTER_ = 1

For variable NAME = DOCSPACK

 TYPE_ = serializable
 REV_ = 2
 TEXT_ = null
 TEXT2_ = org.camunda.bpm.engine.variable.impl.value.FileValueImpl
 SEQUENCE_COUNTER_ = 2

I have also checked the source code of the html. I saw the link <a cam-file-download=“infoDoc”></a> got converted to

<a cam-file-download="infoDoc" href="/camunda/api/engine/engine/default/task/<task_id_from ACT_RU_TASK>/variables/infoDoc/data">C drive space.txt</a>

But the link <a cam-file-download=“DOCSPACK”></a> remained as it is.

Also I tried to download the file attached to infoDoc variable using the url I got from html source

http://localhost:7070/camunda/api/engine/engine/default/task/<task_id_from ACT_RU_TASK>/variables/infoDoc/data

And I was able to download the file.

But when I tried downloading file attached to variable DOCSPACK using following url

http://localhost:7070/camunda/api/engine/engine/default/task/<task_id_from ACT_RU_TASK>/variables/DOCSPACK/data

I got the following error in the browser

{"type":"InvalidRequestException","message":"Variable 'DOCSPACK' is not of type 'Bytes' or 'File' but of type 'object'."}

I have tried quite a few things but nothing seems to be working. I guess file uploaded from form and file uploaded via a Java class end up with different attributes in the ACT_RU_VARIABLE table. The obvious difference noticed is the TYPE for DOCSPACK variable which is serializable and not file. The code mentioned about creating a FileValue object is the standard code suggested by Camunda to use in these scenarios but somehow it is not working.

I would appreciate if somebody can help me as I can’t see light at the end of the tunnel.

Perhaps when instead of doing this, try reading it as an input stream and pass the byte array to .file().
I also had only 1 magical combination working for me… The first PDF I stored was empty

Thanks for replying nvanbelle.

As suggested by you I have tried the following but the result or rather the no result is the same.

Path path = Paths.get(“C:\tmp\files\docs-pack.zip”);
byte[] data = Files.readAllBytes(path);

typedFileValue = Variables.fileValue(“docs-pack.zip”).file(data).mimeType(“application/zip”).encoding(“UTF-8”).create();

Can you please share the code that worked for you. I don’t know what more to do to resolve this issue.

I use IoUtil from camunda for this: org.camunda.bpm.engine.impl.util.IoUtil

InputStream myDocInputStream = MyClass.class.getClassLoader().getResourceAsStream("documents/dummy.pdf");
byte[] bytes = IoUtil.readInputStream(myDocInputStream, "some-document");
delegateTask.setVariable("someDocument", Variables.fileValue("dummy-doc.pdf").file(bytes).encoding("UTF-8").mimeType(MimeType.PDF.getType()).create());
IoUtil.closeSilently(myDocInputStream);
1 Like

nvanbelle apologies for replying late on this.

I have tried as advised by you but the error is the same.

Just to better understand, can you please confirm that you had uploaded a file using Camunda JAVA API and then downloaded it from a html page using code which gets converted to a link allowing file download.