how to write a file object on server response and without saving file on server?

前端 未结 4 1078
不思量自难忘°
不思量自难忘° 2021-01-14 03:59

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 hre

4条回答
  •  广开言路
    2021-01-14 04:20

    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

提交回复
热议问题