4

I am using Spring with DWR . I want to return a file object as response , however I save the file (to be sent) at server temporary location and then send its location as href for anchor tag on client side , however I wonder if there could be a way to throw the file directly to browser on response object without saving it temporarily on server.

I expected if there could be a way to send file as a response via DWR.

Sankalp
  • 2,030
  • 7
  • 30
  • 41

4 Answers4

5
public ModelAndView writeFileContentInResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {

        FileInputStream inputStream = new FileInputStream("FileInputStreamDemo.java");  //read the file

        response.setHeader("Content-Disposition","attachment; filename=test.txt");
        try {
            int c;
            while ((c = inputStream.read()) != -1) {
            response.getWriter().write(c);
            }
        } finally {
            if (inputStream != null) 
                inputStream.close();
                response.getWriter().close();
        }

        }
Rupok
  • 2,062
  • 16
  • 16
  • But again new FileInputStream("FileInputStreamDemo.java"); needs a saved file .... while i want Not to create a file anywhere rather just write the data on response stream . Could it be done someways ? – Sankalp Sep 14 '11 at 07:16
  • 1
    If your response content is dynamic(string) then put content into ByteArrayInputStream and then write to response->writer chunk by chunk – Pokuri Sep 14 '11 at 07:41
  • new FileInputStream("FileInputStreamDemo.java"); is used to read saved file from server temporary location – Rupok Sep 15 '11 at 05:38
0

It has been years since I've used Spring, and I'm unfamiliar with DWR, but the essence of your question is basic to the web.

The answer is yes, you can. In effect, you need to set the HTTP header Content-Disposition: attachment, then stream down the contents. All of this will be in the response to the original request (as opposed to sending back a link).

The actual code to achieve this will depend on your circumstances, but this should get you started.

jimbo
  • 11,004
  • 6
  • 29
  • 46
  • DWR is direct web remoting , we just call a method on server and reponse is send back to it , I expected if there could be a way to send file as a response . – Sankalp Sep 14 '11 at 07:00
0

you call the method from Java Script, right? I didn't really understand how Spring is related in this flow, but as far as I know DWR allows you to produce Java Script Stubs and call the Java methods of the exposed bean directly on server right from your java script client code.

You can read the file byte-by-byte and return it from your java method as long as it really returns a byte array. However what would you do with this byte array on client?

I just think in this specific flow you shouldn't use the DWR but rather issue an ordinar AJAX request (if DWR can wrap it somehow for convenience - great). This request shouldn't come to DWRServlet, but rather be proceeded by a regular servlet/some web-based framework, like Spring MVC :) Once the request comes to the servlet, use

response.setHeader("Content-Disposition","attachment; filename=test.txt");

as was already stated.

Hope this helps, Good luck! Mark

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Absolutely correct Mark , I do call by js and spring is independent of it And I am searching the solution for read some data from server and response back to client with say excel file , without the file saving on server anyway . And yes DWR call not necessary even if I get a AJAX send request ...will work fine. Solutn by you says "You can read the file byte-by-byte and ..." , dear mark I dont want to write any file anywhere except on response without saving it. – Sankalp Sep 14 '11 at 07:28
  • 1
    No, I think I just could express clearly my exlanation :). I meant just that if you _read_ the file on server in the method of bean exposed by DWR, for example: class MyBean {byte [] f() { return read("file.txt" }} - this will send the file to java script side, but then again you can't do anything useful with it, so as for me this is not a solution. This is what I was trying to say. Anyway, I believe, Ajax without the DWR is a way to go here. – Mark Bramnik Sep 14 '11 at 07:54
  • 1
    If you just don't want to save the file on server - well actually you are not required to do so. once you have a byte stream in memory you can send it to client and you're not required to store it on hard drive. The trick is that you don't send the but send the file directly. When browser sees the attachement it wil l open the program to handle this file / offer to download it) This way, the file is not stored on server... – Mark Bramnik Sep 14 '11 at 08:06
-1

An example which return a excel to download from client:

//Java side:

public FileTransfer getExcel(Parametros param){
   byte[] result = <here get data>;
   InputStream myInputStream = new ByteArrayInputStream(result); 
   String excelFormat = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
   FileTransfer dwrExcelFile = new FileTransfer("excel.xlsx", excelFormat, myInputStream);
   return dwrExcelFile;
}

//Javascript side:

function downloadExcelFile() {
  dwr.engine.setTimeout(59000);
  var params = <params_to_send>;
  <Java_class>.getExcel(params, {callback:function(dataFromServer) {
    downloadExcelCallback(dataFromServer);
  }});
}

function downloadExcelCallback(data) {
   dwr.engine.openInDownload(data);
}
fjgarzon
  • 44
  • 4