Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Refreshing page after download
Welcome, Guest. Please login or register.
November 21, 2008, 10:25:01 AM
11306 Posts in 1249 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Serverside Technology  |  PHP  |  Refreshing page after download « previous next »
Pages: [1]
Author Topic: Refreshing page after download  (Read 378 times)
mishkad

Offline Offline

Posts: 15


« on: March 12, 2007, 12:39:13 PM »

Hi,
I have php page with table of list of files, download link and download status(yes/no).
Download link initiates header to open download dialog window:
header("Content-Disposition: attachment; filename=$name");
On download complete the code updates respective file record at status field in DB table to "Yes". So actually it works fine but once download had been completed browser displays file list page with the same status values as before download was taking place, so I need to refresh the browser to view updated status values.

My question is how can I cause the page to be refreshed by the end of download process( when download dialog window is closed)?

Regards,
M.D.
« Last Edit: March 12, 2007, 12:53:35 PM by mishkad » Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #1 on: March 12, 2007, 01:28:04 PM »

if I were you, I would use the DB to store more than just "yes" and "no" for a download; If you're able to store "pending", then the file list age may check if there are downloads pending on a regular basis, then refresh the page if a status (or all statuses) changes from "pending" to "yes".
Perhaps it's also best to use a session variable so that only the browser connected to the person having done a download in "pending" runs such code.
I suggest to use AJAX to drive the DB polling and refresh the page on the client : it's the most efficient in this case.

alternatively, in stead of displaying the files list again (with outdated statuses) after a download dialog box has been opened, just display "return to the list" soi that when the dialog closes, the user may click on the link... and get an updated page.
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #2 on: March 12, 2007, 06:09:05 PM »

Before you close the child window (download dialog window)...do

Code
Language: javascript (GeSHi-highlighted)
window.opener.location.reload();
Logged

Rod
mishkad

Offline Offline

Posts: 15


« Reply #3 on: March 13, 2007, 10:07:39 AM »

Before you close the child window (download dialog window)...do

Code
Language: javascript (GeSHi-highlighted)
window.opener.location.reload();

Hi & thanks for replies,
Can You clarify it as I must admit I'm a bit of confused.
On file list page (aka p1) I have a link to download page (aka p2) where response header to download attachment is send:
header("Content-Disposition: attachment; filename=$name");
Now, do You mean to place the scrip after response header line?
Shoudn't the script point somehow to page that needs to be refreshed (p1 in my case) - correct me if I wrong, the way I see this script it will refresh a page that script is executed within(p2 in mine scenario)?

Regards,
M.D.
Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #4 on: March 13, 2007, 10:07:56 AM »

the problem is that the "dialog box" is probably the standard windows/browser one on which you've no control like this.
I suspect he's sending thehedaer("content-type") with the appropriate mime type, or application/save or the like. so you solution may not be feasible (despite it's of course a very good solution).
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #5 on: March 13, 2007, 10:12:40 AM »

it appears I was right.

her's my view of your problem :

Quote
On file list page (aka p1) I have a link to download page (aka p2) where response header to download attachment is sent

ok. On page2, do this :
- say BODY OnLoad="SetTimer(downloaddone,2000)" to a function like this : (PS I didn't check the JScript syntax)
Code:
function downloaddone() {
  window.opener.reload();
  window.close();
}
- send header("content-type") + data to browser

even better IMHO : don't use OnLoad with a 3s timer ; use OnUnLoad() on page2's window. This way when the download the user closes the page2's window and voila, page1 updates itself. nasty.
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
mishkad

Offline Offline

Posts: 15


« Reply #6 on: March 13, 2007, 11:11:37 AM »

if I were you, I would use the DB to store more than just "yes" and "no" for a download; If you're able to store "pending", then the file list age may check if there are downloads pending on a regular basis, then refresh the page if a status (or all statuses) changes from "pending" to "yes".
Perhaps it's also best to use a session variable so that only the browser connected to the person having done a download in "pending" runs such code.
I suggest to use AJAX to drive the DB polling and refresh the page on the client : it's the most efficient in this case.

alternatively, in stead of displaying the files list again (with outdated statuses) after a download dialog box has been opened, just display "return to the list" soi that when the dialog closes, the user may click on the link... and get an updated page.
Hey Guys - You're JETS!
I'm still trying to post my responce and You've already made it history.
Make me feel like "Back to the future" movie cast!
So, first let me try with all of this stuff - I'll keep U posted on results.
Neverless, I'm posting my q. on first reply - pls your remarks if it's relevant.

First, thanks a lot for this highly professional architecture.
If I'm not using AJAX (which I agree is more efficient but...) to implement this "pending" state idea - Do you mean run some kind of javascript which implement some
while(true) loop {
sleep(time);
if(no_pendings)
   reload_page();
}

That will cause the page being refreshed even if no downloads were performed( at my case the download traffic volume is low....?!
What I really need is to refresh the page right after download was completed so the user will notice the change in file status on-line.

Also, the "Return to list" will probably work ... thanks, but again can I incode it somehow so redirecting and/or refreshing will be performed without user interaction just after closing dialog download window?

Regards,
M.D.
Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #7 on: March 13, 2007, 12:35:25 PM »

show us the code for "page2"

can we see it working somewhere ?
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
mishkad

Offline Offline

Posts: 15


« Reply #8 on: March 15, 2007, 11:05:44 AM »

Hi to Experts,
I've got it working and it was nasty.

First to clarify the scenario - page2  has no representational layer. It uses only to - send header("content-type") + data to browser.

Regarding:
Quote
alternatively, in stead of displaying the files list again (with outdated statuses) after a download dialog box has been opened, just display "return to the list" so that when the dialog closes, the user may click on the link... and get an updated page.
  I'd tried to implement hereabove solution but with no success.
Putting the "Return to List link" on page2 before header("content-type") and others are send requires setting output_buffer to 'On' in php.ini but...once the link is show up on page2 the download dialog box would not open.  Alternatively, putting this link after headers being send line  cause that download dialog box is opened but the "Return to List" link simply would not show up at all.

With regards to:
Quote
On page2, do this :
- say BODY Unload="SetTimer(downloaddone,2000)" to a function like this : (PS I didn't check the JScript syntax)

Code:
function downloaddone() {
  window.opener.reload();
  window.close();
}
- send header("content-type") + data to browser

even better IMHO : don't use OnLoad with a 3s timer ; use OnUnLoad() on page2's window

Generally speaking I had a feeling that this is the right direction to resolve the issue, but it wasn't exactly applied to my case because page2 doesn't have any HTML content and so <Body OnLoad=...> is not feasible.  It also meanless to use it on page1 as it cause to endless refreshing loop.  What I did tried is to change the trigger event to <Body onactivate=...>, so once the dialog window closed the focus returns to page1 window triggering by this refresh() call
routine (aka downloaddone).  That was better but since I have some other javascripts that are embedded within page1 code (like I have script which checks multiple checkboxes on that page fired by single selection) it caused the the page1 being refreshed even when it's not required.

Finally, what's worked for me is hereunder solution:
At page2 I used following structure:
Code:
<script>
window.location = url(of page1);
setTimeout("window.location.reload(true)",1000);
</script>
<?
header( 'Content-type: ' . $ftype );
header( 'Content-Length: ' . $fsize );
header("Content-Disposition: attachment; filename=$name");
exit;
?>

The time delay seems to be essential otherwise the redirection to page1 occurs immediately, skipping headers send lines in code so the download dialog is not opened. (I'd figured it out because the code copies download file at tmpdownload folder on server before headers are send, by noticing that file was copied but no download window was appeared).
So time delay made it out - have no clue why it acts this way.

Conclusion:
The core of solution is at -
window.location.reload(true);
the thing is to find right place to locate it in code and and generically absorb it within.
Also, other proposed solutions may work depends on case and code design constellation.

Thanks for support and Best Regards,
M.D.
« Last Edit: March 15, 2007, 11:12:04 AM by mishkad » Logged
Pages: [1]
« previous next »
    Jump to: