Download & Javascript

M

Marc Ris

Hi,

Following problem, and I can't find any solution for that:

I have an aspx page, which will (finally) generate an XLS-File which must
(can) be downloaded from the user. After that, the window should close
automatically.

Well, the first part for itself is easy with Response.WriteFile().....
The second one for itself is also easy with JS window.close()

Well, combining them seems to be much complicated. The file will be
downladed, but there's no "auto-close" effect.

Any suggestions ?
WBR,
Marc

<snip>
string script = "<script language='javascript'>window.close()</script>";
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.ContentType = "application/zip;filename=download.zip";
Response.AppendHeader("Content-Disposition",
"attachment;filename=download.zip");
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.WriteFile(zipFile, 0, fi.Length);
Response.Write(script);
if(!IsStartupScriptRegistered("closepopup"))
this.RegisterStartupScript("closepopup", script);
Response.Flush();
</snap>
 
E

Eliyahu Goldin

Response.Write("window.close();");

Eliyahu

Marc Ris said:
Hi,

Following problem, and I can't find any solution for that:

I have an aspx page, which will (finally) generate an XLS-File which must
(can) be downloaded from the user. After that, the window should close
automatically.

Well, the first part for itself is easy with Response.WriteFile().....
The second one for itself is also easy with JS window.close()

Well, combining them seems to be much complicated. The file will be
downladed, but there's no "auto-close" effect.

Any suggestions ?
WBR,
Marc

<snip>
string script = "<script language='javascript'>window.close()</script>";
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.ContentType = "application/zip;filename=download.zip";
Response.AppendHeader("Content-Disposition",
"attachment;filename=download.zip");
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.WriteFile(zipFile, 0, fi.Length);
Response.Write(script);
if(!IsStartupScriptRegistered("closepopup"))
this.RegisterStartupScript("closepopup", script);
Response.Flush();
</snap>
=----
 
M

Marc

Eliyahu,
Thanks for your reply.

Unfortunately, the window remains open. No effect at all. (I already tried
this earlier, and tried again now...)

WBR,
Marc
 
E

Eliyahu Goldin

Is it a popup?

Try putting

<base target=_self>

in the <head> section.

Eliyahu

Marc said:
Eliyahu,
Thanks for your reply.

Unfortunately, the window remains open. No effect at all. (I already tried
this earlier, and tried again now...)

WBR,
Marc
=----
 
M

Marc

Well, yes, it's a popup, but, also this kind of "self-reference" has no
effect.
I'm pretty sure, this has something to do with the Download-Code and its
Content-Type.
Quite annoying.

WBR,
Marc
 
G

Guest

Hi Marc,

I had a similar problem and came across on following solution so see if you
can use it.
Quote:
It closes a popup window once the file save dialog has been closed, etiher
by saving the file or clicking cancel, etc.
GenerateCSV.asp generates a csv file using Response.ContentType =
"text/plain" and Response.AddHeader "content-disposition", "attachment;
filename=""" & fileName & """"
End Quote
This should be your page - page that creates file (download page).

First open page with following javascript code - this page will call your
download page:
<script type="text/javascript">
function getCSV(){
location.href="GenerateCSV.asp";
}
function hasFocusBack(){
if(window.focus = true)
window.self.close();

setTimeout("hasFocusBack()", 1000);
}
window.onblur = function(){
hasFocusBack();
}
</script>
....
<body onLoad="javascript:getCSV();">

The trick is, after download dialog is closed, first window gets focus
again, and then it is closed.
I tried this on aspx page and it worked.
 
G

Guest

Hi Marc,
I had a similar problem and came across to following solution. See if you
can use it.

Popup window has following javascript:
function getCSV(){
location.href="GenerateCSV.asp";
}

function hasFocusBack(){
if(window.focus = true)
window.self.close();

setTimeout("hasFocusBack()", 1000);
}

window.onblur = function(){
hasFocusBack();
}

and put in body tag: onLoad="getCSV();"

Popup window opens GenerateCSV.asp page - it can be any aspx page which
dinamically generates some file. In example I found, GenerateCSV.asp
generates a csv file using Response.ContentType = "text/plain" and
Response.AddHeader "content-disposition", "attachment; filename=""" &
fileName & """"
It is important that generated file is opened as attachment.

I hope this will help you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top