Force File Download and then Close Window after save

I would like users to download jpg's from my site and force the browser not to display the image but to promt the user to save it.  

I am almost there, i just have one problem—I can not get the window that is open through this process to close once the download is complete.  This is what i have so far:

1. Button on window1 opens window2
2. window2 has the following code:
     // fetch the file
     String filename = "SCANDISK.LOG";
     String filepath = "C:\\";
          "attachment; filename=\""
               + filename + "\""); fileInputStream =
               + filename);
     int i;
     while (( != -1) {

3. user is prompted to save the file
4. after saving the file the window remains open.
5. source code for window2 at this point is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>

As you can see by the code, i would be unable to add any onLoad commands to close the window.

I have tried running a function from the window1 that tries to close window2 every 2 seconds, this causes an error beep until the user saves the file.  I assume this happens because it is trying to close a window that is currently locked(not sure if this is the right term).

So can anyone figure a way so that i can send this file to a user and either not have a window open or have it close once the file has been transferred?

Who is Participating?
JgouldConnect With a Mentor Commented:
User asked for deletion, anyone objecting?

i would say PAQ and refund

JGould-EE Moderator
why do you have to open a second window.. the button should just send the user to your JSP and that will prompt for a download right? No need for a second window or am I missing something?

menreeqAuthor Commented:
hmmm...maybe i am missing something.  So what changes would make?
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

in your html how about this:

<input type="button" onClick="window.location.href='yourdownloadjsp.jsp';return true;" value="Download File Now!">

menreeqAuthor Commented:
Yes I think there is something missing but it is my fault for not including it.  What you have suggest is fine, however now it leaves window1 (the only window) open after the download is complete...I would like to have this window close after a download (if possible I would like to have it close only after a successful download, I mean only when the user selects saveas and then ok).  Do you know how I can do this?
how about closing the window after about 10-15 seconds, after you sure that the user has selected to download the file.

I haven't found any means of getting info from the dialog itself to JavaScript.

menreeqAuthor Commented:
I tried that, but i was unhappy with the results, sometimes it closed to late and sometimes it tried to early and you would hear an anoying error beep.  I think i will just have to leave the window open...

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.

All Courses

From novice to tech pro — start learning today.