This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Save File on Client Machine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Save File on Client Machine" Watch "Save File on Client Machine" New topic
Author

Save File on Client Machine

carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

I want to save file created from application running on server machine on the client machine , i am creating excel file using POI library now i want to store the created file on client machine.

Export.jsp---- containd button export to excel, which calls a servlet ExportExcel.java where i am creating excel file

ExportExcel.java



from teh above code the file is being saved on the server machine's D: but i want to save it on the clients machine(who is accessing the application) D:

Also i want to provide and option of save once the file dynamically gets generated ... as happens when we write something like content type=vnd.msexcel .

Can anybody suggest me please how do i do this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
You need to stream the file contents through the response output stream, using the HSSFWorkBook.write method. See the "Simple Stream" example in http://faq.javaranch.com/java/CodeBarnServlets for how to do that. And, yes, you need to set the appropriate content type.


Ping & DNS - my free Android networking tools app
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

i modified my program but it does not give me the desire result



here once i export to excel the file gets saved on the server machines' D: not on my machine also i want the option that once the excel file is written i want to provide the user with an option of open save cancel . can naybody please suggest me how do i do this...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Instead of writing the file to disk, and then using getResourceAsStream, you should stream the workbook contents directly to the servlet output stream, using the method I mentioned earlier.

Then the browser will prompt the user what to do with the file.
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

I am calling the export to excel file using JQuery

$.post("../reports/ExcelExport",{csvstring: $("#vname1").val(),count:$("#vname2").val(),propType:$("#vname3").val(),instValue:$("#vname4").val()},function(data){
alert(data);document.body.style.cursor="default"; });


The open save and cancel dialogue box is not appearing, but when i directly call the servlet as

http://localhost:8080/AI/reports/ExcelExport?csvstring='3490000526','3500000367','3501000636','3510000467','3511000736','3520000567','3521000836','3530000096'&count=8&instValue=ALL&propType=BOTH

the dialogue box open save and cancel opens ...

how do i handle this while using jQuery can anybody suggest me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
You can't load an XLS file through AJAX. You'll need to do that through an "href" link of some kind.
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

how can i provide a link to file that is dynamically created, do i need to store it somewhere on the client machine .... if yes how could i store the files on client machine that are created at runtime through a servlet ., also once the file gets created at runtime i want to open the file and display its contents , can any body suggest me how do i do this.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
There's a big misunderstanding here.

You need to distinguish between "server side" and "client side". Those are in fact two physically separate machines with each physically an own file system. On each side the path "c:/foo/bar.ext" would point to the local file system.

Imagine that I am the client and that you are the server, how would you be able to access my local file system? I have a file at c:/documents/picture.jpg, are you able to access it? Would the server machine be? Think about it.

To send a file from the server to the client, the client need to request it and the server need to send the file with the response. In the server side you get the file contents as an InputStream and you write it to the OutputStream of the response. That's all. To pop a 'save as' dialogue, you need to set the content-disposition header to "attachment".
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

My main problem was to call a servlet using AJAX and then show the client open save and cancel dialogue box.... and i solved it

by invoking "../reports/BillIDPropIDExcelExport servlet i saved the excel file in a public folder and returned back the filename, now in the function(data)
you can see i am invoking another servlet called OpenFile which just take the file name and open the file from public folder. A TimeBeing Solution :idea:


$.post("../reports/BillIDPropIDExcelExport",{csvstring: $("#vname1").val(),count:$("#vname2").val(),propType:$("#vname3").val(),instValue:$("#vname4").val()},function(data){ document.body.style.cursor="default";

document.getElementById('if1').src="../OpenFile?filename="+data;
});
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

Still i have a Doubt when server cannot access the client local file system.... now if i dont want to show the open save and cancel box just directly save the file on client machines local file system and when i write like this where should the FileOutputStream point to



HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Customer Details Connection Fee");


FileOutputStream fileOut = new FileOutputStream("C:\\Temp\\ExtractBillID"+dateFormat+".xls");
wb.write(fileOut);
fileOut.close();
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

ruquia tabassum wrote:Still i have a Doubt when server cannot access the client local file system.... now if i dont want to show the open save and cancel box just directly save the file on client machines local file system

Cannot be done. Imagine the security ramifications if it could!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
ruquia tabassum wrote:Still i have a Doubt when server cannot access the client local file system.... now if i dont want to show the open save and cancel box just directly save the file on client machines local file system and when i write like this where should the FileOutputStream point to


You don't understand it? First forgot about Java at all and imagine that I am here and I have a folder at "c:/foo/bar" and that you are there and have a file "file.ext", how would you write it to my disk right now? If you can, just do the same thing with Java. If you can't (you can't), then you can't do it with Java either. Also try to think about why that isn't possible.
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

ok... if the file gets copied itself without the permission or knowledge of the user then it would resemble a virus . Thanks for making things clear .
 
jQuery in Action, 2nd edition
 
subject: Save File on Client Machine