cancel
Showing results for 
Search instead for 
Did you mean: 

How to stream a document to the browser

tonzon
Champ in-the-making
Champ in-the-making
Hi,

I have a problem when trying to stream a .txt file to the client. The browser pop-up never appear… (I can actually see the text in the response tab in firebug - so the client has the data).

My code is as follows…

WebScript:

<webscript>
  <shortname>Create text file</shortname>
  <description>some text</description>
  <url>/abc/createtxtfile</url>
  <format default="text">extension</format>
  <authentication>user</authentication>
</webscript>

Java:

public void execute(WebScriptRequest request, WebScriptResponse response){
   String filename = "C:\\temp\\myFile.txt";

   response.setContentType("text/plain");

   String disHeader = "Attachment;
   Filename=\"myFile.txt\"";
   response.setHeader("Content-Disposition", disHeader);

   File fileToDownload = new File(filename);
   FileInputStream fileInputStream = new FileInputStream(fileToDownload);
   int i;
   while ((i=fileInputStream.read())!=-1)
   {
      out.write(i);
   }
   fileInputStream.close();
   out.close();
}

Javascript:
successcallback:{
    fn: fnSuccess
    scope: this
}

var fnSuccess = function onSuccess(){
   //doing nothing right now…
}

I hope there is someone out there that can help. Thanks in advance…
1 REPLY 1

openpj
Elite Collaborator
Elite Collaborator
Try to replace this string about the HTTP header:

response.setHeader("Content-Disposition","attachment;filename=\"" + filename + "\"");
Hope this helps.