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

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
0
yabbatim
Asked:
yabbatim
  • 14
  • 10
  • 5
2 Solutions
 
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
 
objectsCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
objectsCommented:
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
 
objectsCommented:
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
 
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
 
objectsCommented:
> 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
 
objectsCommented:
> 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
 
objectsCommented:
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
 
objectsCommented:
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
 
objectsCommented:
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
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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