Solved

Save as function

Posted on 2003-11-27
29
507 Views
Last Modified: 2013-11-24
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
Comment
Question by:yabbatim
  • 14
  • 10
  • 5
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:yabbatim
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
And you should set the Content-Length too
0
 

Author Comment

by:yabbatim
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to get it from somewhere, unless you create it on the fly.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>without keeping a copy on the server

There's no 'copy' of the original needed.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 

Author Comment

by:yabbatim
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
AFAIK the only time it's good practice to omit C-L is when it cannot be included
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>write it directly to the servlets response stream

That's what i said earlier of course.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:yabbatim
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
Comment Utility
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
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
Comment Utility
The code posted above will do what you require, removing the need for creating a file on your server.
0
 

Author Comment

by:yabbatim
Comment Utility
Cool. Thank you both. Is there a way to give you both points?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Click on the split points link
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now