• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Save File on Client Machine

 
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic