cancel
Showing results for 
Search instead for 
Did you mean: 

Return file from js wbscript

vladislavlysov
Champ in-the-making
Champ in-the-making
Hello!
  I have a problem. I need get file from js webscript. But I didn't found solution. Maybe, anybody know, how to solve this problem?
Thanks.
5 REPLIES 5

mitpatoliya
Star Collaborator
Star Collaborator
I really do not think you can actually return the whole file.
What you can do is return the download URL of that file.
That will serve your purpose I guess.
If not let us know the exact scenario.

scouil
Star Contributor
Star Contributor
Does it have to be a webscript or can it be a servlet?
Each ScriptNode has 2 attributes in javascript .url and .downloadUrl, to respectively open the document and to download it.
http://wiki.alfresco.com/wiki/4.0_JavaScript_API#ScriptNode_API

But those redirecting to those will trigger the expected effect but since those are servlets you'll have to log again if you're only logged in for webscripts.

There is a webscript that already exists that will display the document if your browser can read it or dowload it if it can't.
http://wiki.alfresco.com/wiki/Web_Scripts_Examples#Folder_Browse.2FRSS_Feed
If the path is a folder it will display the folder content, if it's a document, it will display the document.

Hope this helps,

vladislavlysov
Champ in-the-making
Champ in-the-making
Thanks for response? But for my problem i need have file not in Alfresco! For now i wrote 2 webscripts(first on Java, second - on javascript). And now i get another problem. I need execute js webscript from Java webscript.  I know, how do it:

req.getRuntime().getContainer().getRegistry().getWebScript("com/home/testJs/testJs.get").execute(req, res)
, but how to construct the new WebScriptRequest object? I need do it for rewrite request path. It's a really problem for me.
Thank you.

vladislavlysov
Champ in-the-making
Champ in-the-making
Hello again.
  I resolve my problem. But now i have another issue. I have Java webscript(in Alfresco, not Share), which return file (for this I set response header to "Content-Disposition", set content type to "application/octet-stream" and write file to response output stream). From Share I do AJAX post request to Java webscript. After that I expect to get the file for download in browser. But actually - nothing happens. In debug mode in my java webscript I see - all is ok. But file not downloaded. I not understand why… Maybe anybody know?
Thank you.

vladislavlysov
Champ in-the-making
Champ in-the-making
For now, I have the next code, which not worked correctly:

public class ExportToExcelWebScipt extends AbstractWebScript {

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException {
        File resultFile = exportToExcel(req.getContent().getContent());
        res.setContentType(MimetypeMap.MIMETYPE_EXCEL);
        res.setHeader("Content-Disposition", "attachment");
        res.setHeader("Content-length",  String.valueOf(resultFile.length()));
        FileContentReader reader = new FileContentReader(resultFile);
        reader.setMimetype(MimetypeMap.MIMETYPE_EXCEL);
        reader.getContent(r.getOutputStream());
    }

}
When I search solution in Alfresco source code, I see, what for download file from Alfresco via Share used CoyoteResponse(in my case I'm used BufferdResponse) and then I'm try to execute next code:

public class ExportToExcelWebScipt extends AbstractWebScript {

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException {
        File resultFile = exportToExcel(req.getContent().getContent());
        FileContentReader reader = new FileContentReader(resultFile);
        reader.setMimetype(MimetypeMap.MIMETYPE_EXCEL);
        HttpServletResponse coyoteResp = ((WebScriptServletResponse)((WrappingWebScriptResponse) res).getNext()).getHttpServletResponse();
        coyoteResp .setContentType(MimetypeMap.MIMETYPE_EXCEL);
        coyoteResp .setHeader("Content-Disposition", "attachment");
        coyoteResp .setHeader("Content-length",  String.valueOf(resultFile.length()));
        reader.getContent(coyoteResp.getOutputStream());
    }
But I got the same result. I don't know - what I did wrong. Maybe anybody know?
Note - this code return file correctly(I see it in Fiddler), but it's don't download in browser.
Thank you.