Save as function

I have used the zip api to dynamically generate a zip file from input files that i have accessed through database references. However the file automatically saves itself to the root directory of jakarta tomcat 4. How do i go about setting up a save as dialog box that will allow me to change the directory that the file is saved in? Does anyone know where a tutorial for this sort of stuff is on the net? Any help would be greatly appreciated. TIA. Adrian
yabbatimAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You can't really do that as any dialog would appear on the client, which has nothing to do with the path. If it didn't appear on the client, how and where could it be seen?
0
Mick BarryJava DeveloperCommented:
You could prompt them for a relative location to use, and then use that to save it to an are on your server. Giving the user access to save it anywhere on your server probably isn't the best idea :)
0
yabbatimAuthor Commented:
I actually want it to save on the client machine. Sorry for the confusion. So, as a user i need to generate a zip file to save to my machine. Is this possible? Adrian
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Mick BarryJava DeveloperCommented:
yes, you just need a servlet to return the file for downloading. The browser will prompt the user for a location to save. have a look at download option at http://www.objects.com.au/products/jstart/index.jsp for an example of how it works client side.
0
Mick BarryJava DeveloperCommented:
your servlet should set the following headers:

response.setContentType("application/octet-stream" + "; name=\"" + filename + "\"");
response.setHeader("Content-Disposition","attachment; filename=" + filename +";");

Then just write the file to the response output stream.
0
Mick BarryJava DeveloperCommented:
0
CEHJCommented:
And you should set the Content-Length too
0
yabbatimAuthor Commented:
Is there a way to just save it on the client machine without keeping a copy on the server? As in I select the files for the zip, choose a destination, click save and it is saved on the client machine only? Thanks
0
CEHJCommented:
You need to get it from somewhere, unless you create it on the fly.
0
CEHJCommented:
>>without keeping a copy on the server

There's no 'copy' of the original needed.
0
Mick BarryJava DeveloperCommented:
> And you should set the Content-Length too

Don't believe that is necessary

> Is there a way to just save it on the client machine without keeping a copy on the server?

Just remove it from server after download.
0
CEHJCommented:
>>Don't believe that is necessary

It's not absolutely essential, but it's not good to have the client working in the dark.
0
yabbatimAuthor Commented:
It is being generated on the fly. I have a search results page of items in my database. I select the ones that i want to store in the zip and i press generate. This generates a zip file that saves in the root directory of tomcat on the server. What i want is for it to save in a directory on the client machine that the current user selects. Sorry for the confusion, i am not sure how to explain it better. Adrian
0
CEHJCommented:
Oh, OK. Then you can't set the Content-Length. You could simply use a zip stream and dispense with any file other than on the client
0
CEHJCommented:
Oh, OK. Then you can't set the Content-Length. You could simply use a zip stream and dispense with any file other than on the client
0
CEHJCommented:
Sorry - wanted to alter that slightly:

If you're *starting* with a file then of course you *could* set the Content-Length, but you can just as easily generate everything on the fly and dispense with Content-Lenght and file
0
Mick BarryJava DeveloperCommented:
> but it's not good to have the client working in the dark.

Not sure what you mean by working in the dark, it is simply reading the required number of bytes. I doubt any client uses content length to create a buffer to store the entire contents in. Read the RFC for more info on the spec.
0
Mick BarryJava DeveloperCommented:
Instead of saving the generated data to a file, write it directly to the servlets response stream. The client will then be prompted with a dialog for where to save it.

0
CEHJCommented:
AFAIK the only time it's good practice to omit C-L is when it cannot be included
0
CEHJCommented:
>>write it directly to the servlets response stream

That's what i said earlier of course.
0
Mick BarryJava DeveloperCommented:
You servlet would look something like the following:

response.setContentType("application/octet-stream" + "; name=\"" + filename + "\"");
response.setHeader("Content-Disposition","attachment; filename=" + filename +";");

InputStream in = getZipStreamFromDatabase();
OutputStream out = response.getOutputStream();
int n;
while (-1!=(n=in.read()))
{
   out.write(n);
}
in.close();
out.close();
0
yabbatimAuthor Commented:
I am lost. I can generate a zip file as it is, that part is working fine. It saves to my server machine as mentioned before. I dont want it to do this. When i click generate i was a "save as" dialog box to come up to the client can select a name for the file and a directory to store it in. When they click save i want the program to take the files that i have selected and save them into the directory that the client has selected with the name the client has selected. I am sort of new to all of this stuff. Thanks
0
CEHJCommented:
All we're saying is that, if you don't want it, you can omit placing a temporary zip file at the server. You can generate a zip stream on the fly
0
CEHJCommented:
IOW there are 2 approaches

1. Generate zip file. Get stream and give it to the client to save. Delete file on server.
2. Generate stream and give it to the client to save
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick BarryJava DeveloperCommented:
The code posted above will do what you require, removing the need for creating a file on your server.
0
yabbatimAuthor Commented:
Cool. Thank you both. Is there a way to give you both points?
0
CEHJCommented:
Click on the split points link
0
CEHJCommented:
8-)
0
Mick BarryJava DeveloperCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.