Solved

Save as function

Posted on 2003-11-27
29
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 10
  • 5
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9834121
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
ID: 9834126
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
ID: 9834158
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 9834170
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
ID: 9834173
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
ID: 9834186
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9834187
And you should set the Content-Length too
0
 

Author Comment

by:yabbatim
ID: 9834188
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
ID: 9834196
You need to get it from somewhere, unless you create it on the fly.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9834202
>>without keeping a copy on the server

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

Expert Comment

by:objects
ID: 9834203
> 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
ID: 9834212
>>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
ID: 9834226
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
ID: 9834244
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
ID: 9834248
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
ID: 9834256
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
ID: 9834258
> 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
ID: 9834264
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
ID: 9834268
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
ID: 9834273
>>write it directly to the servlets response stream

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

Expert Comment

by:objects
ID: 9834295
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
ID: 9834328
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
ID: 9834340
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
ID: 9834344
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
ID: 9834355
The code posted above will do what you require, removing the need for creating a file on your server.
0
 

Author Comment

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

Expert Comment

by:CEHJ
ID: 9834364
Click on the split points link
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9834379
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9834384
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

635 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