• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Save As Dialog

Hello i have this code that generate me an csv file, this file is saved in the C:\ of my server, I would like to give the user the option to save the file in their machines with a Save as Dialog option

down is the code i use to generate the file


BufferedWriter writer = new BufferedWriter(new FileWriter("C://file.csv"));
 
	       
	         for (int i=0;i<arrayExportXLS.size();i++){
	        	 
	         
	        	 ExportXLS expi = (ExportXLS) arrayExportXLS.get(i); 
		        	 writer.write(expi.printObjectI()); 

		             writer.newLine();
	         }
	       
			 writer.close();

Open in new window

0
cano63
Asked:
cano63
  • 14
  • 11
1 Solution
 
for_yanCommented:
If you want to use the Save As dialog you should use the JFileChooser comopponent
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
for_yanCommented:


And this one with a little bit of explanation:
http://www.javaswing.org/jfilechooser-example.aspx
0
 
for_yanCommented:
If you are writing about web application, then you don't need to worry about it as your browser
will normally open similar finctionality and allow the user to save the file in the local folder, which
user chooses.
0
 
cano63Author Commented:
I,m working in a web application, is not supposed to be created with java script?
0
 
for_yanCommented:
No, most of the content on the web is actually in HTML, only some more sophisticated functionality is dine in JavaScript. The task you mentioned does nor require JavaScript - when you have a link pointing to your CSV browser will normally show your client  filechooser window - this is part of the bropwser fuincionaltiy/standard configuration
0
 
cano63Author Commented:
But how can i generate that save file automatic from the file that i create, let said i create the file and automaticly display the user the save dialog of the browser, how i pass my file function
0
 
for_yanCommented:
You just place the link to your file on your Web page - then the behavior will all depend on the setup of the client browser.
Most browsers are set up in such way that if link points to .html file - it will just display it,
however if link points to file with most of other extensions  - say to .doc, to .xls or most of the other
it will first show the dialog asking if you want to open or to save and when you select "save" it will
open this Save As dialog as it is presented by the browser, so then user will be able to select in what folder
she wants to store the file. This behavior may vary with the browser or setup but this is how at least IE is usualy set up.
0
 
cano63Author Commented:
i don't want to place the link of the file in my application i want it to generated automaticly

I have this in my servlet

      response.setContentType("Application/csv");
      response.setContentLength(bytes.length);
                        servletOutputStream.write(bytes, 0, bytes.length);
                        servletOutputStream.flush();
                        servletOutputStream.close();

THis is suposed to show me the save as dialog, but how i pas to it my file
0
 
for_yanCommented:
I'm not sure  I understand.
Thsi is the code on the server side - it would not show anything because it is running
in closed server envirtonment. Server code usually does not have any
visual front end - whatever folder you specify in the code
or whatever folder name is passed to your server as parameter can be usesd to write the file.
But server code would normally not interact with the user.
Please, explain who you think would be browsing for the output folder to save the data
 - do you want the users of your application on the client side to do it?

In normal situation in the server you can generate file name for the server side file where you write
some stuff which your servlet generated - say the picture or table with data. Then you place the link to
thus written file on your web page.
When the user cllicks on that link and then browses to the folder on his local machine
then browser will download the contents of the
file which you prepared on the server and will download itto the local
file of the user/client which she chose.

If you mean something else, then please explain.
0
 
cano63Author Commented:
My file is already created in the C:\directory of the server, i want the user to be able to download it, but not with a link i want to call the function when he press a button.

I think i can hit a servlet and the servlet will return the save dialog
0
 
for_yanCommented:

Well, I usually don't very much subscribe to idea of bad practices, etc.
but if there is any really bad practice - that would be to show any kind of dialog from
the servlet code.

Servlet runs on the server - most of your users woiuld not run anything on the server.
They usually access the server through browser.

If you want to do something on the server side for those who have direct access to server -
then you don't need servlet - just write java application and show JFileChooser dialog as we were talking in the very beginning.
The main purpose of the servlet is to create html page which should be
accessible to users who are not running anything directly on the server

0
 
cano63Author Commented:
The button will be in a jsp, that jsp will call the servlet
0
 
for_yanCommented:
That part is fine. And what will happen next?
0
 
cano63Author Commented:
There where i need to call my file an present the user the option to save the file, that where i,m lost
0
 
for_yanCommented:
Who will be the user - at what computer he/she will be sitting?
Will they be directly wortking on your server?
0
 
cano63Author Commented:
NO  like a web application could acced from any place the file is saved in the server
0
 
for_yanCommented:
How can she access from any place your file on the server?
To do that after you create the file on the server you return to the user the html page containing link
to that file. When user clicks that link browser opens Save As dialog.
If you open diallog sirectly in the servlet code which runs on teh server this dialog would be
seen only to those logged in deirectly to the server
0
 
cano63Author Commented:
And how i can return that link to the user using the save as dialog of the browser
0
 
for_yanCommented:

You just put link in your page like that:

1 records retrieved. Click <a href="../images/gifs/1300826636110.csv">here</a> to get the output in MSExcel format.

Open in new window


This is you path and filename on the server (but here you should be attantive it should start from server doc root) :  ../images/gifs/1300826636110.csv


The user will see ioon the page:

1 records retrieved. Click here to get the output in MSExcel format

but the word "here" would be underlined as link.
When the user clicks on it IE  will open window asking
do you want to save open, etc. Then when you say Save, IE
will open Save As dialog

Firefox has different strategy - it usually does not ask the folder,
and stores in the download folder, but perhaps it still can
be configured. Anyway this is more or less standard way users acces files
from any web sites - they would be accustomed to this method, whatever browser they use,
and you would hardly want to change this practice






0
 
cano63Author Commented:
There is a way to generate that dynamic, not putting it in the link in the jsp,

Click a button and the button do everything?
0
 
cano63Author Commented:
I found it
FileInputStream fis = new FileInputStream("C://file.csv");
				 
				
				 ByteArrayOutputStream bos = new ByteArrayOutputStream();
				 byte[] buf = new byte[1024];
				 try {
				 for (int readNum; (readNum = fis.read(buf)) != -1;) {
				 bos.write(buf, 0, readNum); //no doubt here is 0
				
				 System.out.println("read " + readNum + " bytes,");
				 }
				 } catch (IOException ex) {
				
				 }
				 byte[] bytes = bos.toByteArray();

				 
				 ServletOutputStream servletOutputStream = response.getOutputStream();

					response.setContentType("Application/csv");
					response.setHeader("Content-Disposition", "attachment; filename=file.csv");
					response.setContentLength(bytes.length);
					servletOutputStream.write(bytes, 0, bytes.length);
					servletOutputStream.flush();
					servletOutputStream.close();

Open in new window

0
 
for_yanCommented:
Fine, if that is what you want.
I guess, now I understand what you meant: you don't want to show one more page with the link,
but want to send file back directly without the intermediate page, immediately as a result of
the work of your servlet.  Yes, I guess, that should be possible with modification of  the header.
0
 
cano63Author Commented:
I found the aswer of what i was looking for.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 14
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now