cancel
Showing results for 
Search instead for 
Did you mean: 

How to send webscript response as file (file download box)

g_rathod
Star Contributor
Star Contributor
Hi,

In alfresco 4 share enterprise, When I perform advance search, I place one more button called "Export Search", when search result gives me nodes (searched results).
e.g. Let say I got 5 results. Following are the steps I performed.
1) Get 5 nodeRefs from client side search.js
2) Calling repo side webscript from the same search.js (custom repo side webscript)
3) My repo side webscript will process those nodes and write data to one file (export as csv)
4) Now question is what output should I give in desc/or response file so, it will directly ask user to save/open that csv file ????


Let me know if anybody resolved same kind of thing.
15 REPLIES 15

erikwinlof
Confirmed Champ
Confirmed Champ
Make sure you get your webscript to set the "Content-Disposition" header to something like "attachment; filename=MyExcelDoc.xls".
As reference take a look at the org.alfresco.repo.web.scripts.content.StreamContent & org.alfresco.repo.web.scripts.content.ContentGet classes.

Cheers, Erik

g_rathod
Star Contributor
Star Contributor
Hi Erik,

Thanks for quick reply.
Actually I am using "extends DeclarativeWebScript" so response is not available in it.
Sorry but I could not understand from given example you have suggested. Is there any more snippet which is used in DeclarativeWebscript ?

erikwinlof
Confirmed Champ
Confirmed Champ
Then I would suggest you extend the org.alfresco.repo.web.scripts.content.StreamContent instead of DeclarativeWebscript.
Take a look at how the org.alfresco.repo.web.scripts.content.ContentGet as an example.

Cheers,

:: Erik

g_rathod
Star Contributor
Star Contributor
Thanks Erik,
One more question, How my desc.xml looks like then ??

Example like :
<webscript>
  <shortname>Export Search Results</shortname>
  <description>Export Searching result in Advance Search</description>
  <url>/repository/exportcsv</url>
  <format default="json"></format>
  <authentication>user</authentication>
</webscript>

for file how it should be?

erikwinlof
Confirmed Champ
Confirmed Champ
Take a look at the desc file for ContentGet.java:

http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/remote-api/config/alf...

You should for example change your <format> declaration, to i.e.  <format default="">argument</format>

Cheers, Erik

g_rathod
Star Contributor
Star Contributor
Hi Erik,
Thanks a lot.

Actually this helps. currently in declarative webscript,I thought of writing that csv file in same webscript class to some temp location.
and thought to send it as response.
Now I have two things remaining like how to write a file (temparary location tomcat/temp) and send stream as response locating our file, as you suggested.
so currently being stuck at this stage.

erikwinlof
Confirmed Champ
Confirmed Champ
Please take a look at the ContentGet implementation, in there you will be able to see how its done. I.e. look at

protected void streamContent(WebScriptRequest req, WebScriptResponse res, File file, Long modifiedTime, boolean attach, String attachFileName) throws IOException

to see who streaming from a file is done.

Cheers and good luck, Erik

g_rathod
Star Contributor
Star Contributor
Hi Erik,

Thanks, but what my option is, see when internally I am creating a file can't we expose it directly, here ContentGet implementation seems we need to first store it in alfresco repository and then populating stuffs.
because what I have in return of webscript is File object thats it.
As per implementation I saw in ContentGet is following :

I have checked
protected void streamContent(WebScriptRequest req, WebScriptResponse res, File file, Long modifiedTime, boolean attach, String attachFileName) throws IOException
which is also requiring following :
1) resourcePath
2) attach
3) attachFileName

Actually as I have only File to return.  with No StreamId/ content props/attach….

<!– by object id –>
  <url>/cmis/i/{id}/content{property}?a={attach?}&amp;streamId={streamId?}</url>
  <url>/cmis/s/{store}/i/{id}/content{property}?a={attach?}&amp;streamId={streamId?}</url>
  <!– by path –>
  <url>/cmis/p{path}/content{property}?a={attach?}&amp;streamId={streamId?}</url>
  <url>/cmis/s/{store}/p{path}/content{property}?a={attach?}&amp;streamId={streamId?}</url>

  <!– alfresco style –>
  <url>/api/node/content{property}/{store_type}/{store_id}/{id}?a={attach?}&amp;streamId={streamId?}</url>
  <url>/api/path/content{property}/{store_type}/{store_id}/{nodepath}?a={attach?}&amp;streamId={streamId?}</url>
  <url>/api/avmpath/content{property}/{store_id}/{avmpath}?a={attach?}&amp;streamId={streamId?}</url>
  <url>/api/node/{store_type}/{store_id}/{id}/content{property}?a={attach?}&amp;streamId={streamId?}</url>
  <url>/api/path/{store_type}/{store_id}/{nodepath}/content{property}?a={attach?}&amp;streamId={streamId?}</url>

g_rathod
Star Contributor
Star Contributor
Hi Erik,

Finally I am able to use StreamContent as you said.
streamContent(req, res, tempFile, true, tempFile.getName());

where tempFile =  file Object
true = attachment boolean

but sadly it wont show me save As or download box. inside tomcat no other errors are there.

Even using streamContent, do I need to set following ??

res.setContentType("application/csv");
res.setHeader("content-disposition","filename=test.csv");


Can't we download content without storing it to alfresco repository ???. It is currently stored in my alfresco/tomcat/temp