Solved

Page Refresh after download

Posted on 2004-04-06
20
318 Views
Last Modified: 2010-05-18
I have a page that shows a RadioButtonList associated with a list of files available for the user to download.  The list is databound to an array that I created using the Directory.GetFiles() method.

Once the user selects a file, they can click a button to download that file using the Respose.WriteFile method.  Once the file is downloaded I want to move it from it's current directory on the server to a subdirectory called "deleted" and refresh the page.  Everything seems to work fine except I cannot get the page to refresh, so the file that is no longer available is still displayed.  This is no good.  Any ideas what I am doing wrong here?

Code:

//get file and send to client
ListItem liFile = rbFiles.Items[rbFiles.SelectedIndex];
Response.AppendHeader("content-disposition", "attachment; filename=" + liFile.Text);
Response.ContentType = "application/x-msdownload";
Response.WriteFile(liFile.Value);
Response.Flush();

//move file to deleted subdir
strSourcePath = Constants.FILE_SOURCE_PATH;
string strDestPath = strSourcePath + "\\deleted";
if(!(Directory.Exists(strDestPath))){
    Directory.CreateDirectory(strDestPath);
}
File.Move(liFile.Value, strDestPath + "\\" + liFile.Text);

//refresh the page
Response.Redirect("default.aspx");

Any help is greatly appreciated.
0
Comment
Question by:GEJames
  • 6
  • 4
  • 3
  • +4
20 Comments
 
LVL 7

Expert Comment

by:TheKenman
ID: 10771410
Could try:

     Server.Transfer("default.aspx");

Otherwise you could try it client-side with javascript or meta-refresh...
0
 

Author Comment

by:GEJames
ID: 10771472
Server.Transfer("default.aspx");  doesn't work.

How would I use javascript or meta-refresh so that the page would refresh after the download?
0
 
LVL 1

Expert Comment

by:Rajmahesh
ID: 10772044
try this

Server.Execute("default.aspx");

also check this site for more info

http://www.ondotnet.com/pub/a/dotnet/2003/04/07/aspnetnav.html

kenman...i heard that meta-refresh is not a good option to be used in pages for redirecting.i donno much of that.sorry if im wrong
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 10772061
The answer would be no. Unless you are using some activex control to manage the download or any Thick client application Like CuteFTP. Since the file is downloaded on the client, there is no way to track if the file has been downloaded completely or not because, a request cannot be send back to the server indicating the download status.

However, You can do a refresh of the page when the download is initiated.

Cheers!!
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 10772106
I'm at a loss at why Response.Redirect and Server.Transfer aren't working for you...

And I just realized that you're working with .NET.. I'm not very good with .NET...or JScript :( But I will offer suggestions since nobody else has responded.

That being said, I don't believe javascript/meta-refresh would be a good solution- they're liable to be buggy for this sort of application.

While looking around, I found something else that just might work perfect for you:

//get file and send to client
ListItem liFile = rbFiles.Items[rbFiles.SelectedIndex];
Response.AppendHeader("content-disposition", "attachment; filename=" + liFile.Text);
Response.ContentType = "application/x-msdownload";
Response.WriteFile(liFile.Value);
Response.Flush();

//move file to deleted subdir
strSourcePath = Constants.FILE_SOURCE_PATH;
string strDestPath = strSourcePath + "\\deleted";
if(!(Directory.Exists(strDestPath))){
    Directory.CreateDirectory(strDestPath);
}
File.Move(liFile.Value, strDestPath + "\\" + liFile.Text);

rbFiles.Items.Remove(liFile);

// rbFiles.Items.Remove(rbFiles.SelectedItem);
// rbFiles.Items.RemoveAt(rbFiles.SelectedIndex);
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 10772111
Well.. make that, nobody had responded when I first pulled up the Q :p
0
 
LVL 2

Expert Comment

by:khurram007
ID: 10772420
For HTML refresh use the code below...

1: Refresh this document

<meta http-equiv="refresh" content="5">

2: Go to URL

<meta http-equiv="refresh" content="5;URL=addedsuggest.asp">

HTH

Khurram
0
 

Author Comment

by:GEJames
ID: 10776406
Thanks for all of your suggestions so far, but I'm afraid none of them have worked.

Rajmahesh - I'm afraid Server.Execute doesn't work.  I would expect it to work the same as Response.Redirect or Server.Transfer, but those didn't work either.

ap_sajith - How do I refresh the page after the download is initiated?  I don't need to wait until the download is finished.

TheKenman - I'm at a loss too as to why Response.Redirect and Server.Transfer don't execute.  I also tried removing the file from the rbFiles array as you've suggested but that doesn't update the page at all.

khurram007 - thanks.   I'd never seen how to do a HTML refresh before.  That's pretty cool.  My page refreshes every five seconds, until I download a file.  At that point it stops refreshing automatically so this doesn't really help me either.

Very strange, I don't see why some of my code after the Response.WriteFile works and some doesn't.  I commented out the four lines of code that actually send the file to the client (from Response.AppendHeader() to Response.Flush()) and all the code ran fine.  Something about downloading a file that messes up everything else on the page...

Please keep the suggestions coming and thanks for your efforts so far.

James
0
 
LVL 2

Expert Comment

by:khurram007
ID: 10781523
ok... let me clearify your question for me...

you want a user to download a file. After the download you want the file to move in the deleted directory, and it is working fine till this stage. The problem starts when the download ends and the current page still displays...

Well what do you want to display in the final page? If you want to transfer your page just after the download finishes, then I am afraid to say that I don't think it is possible... Even the biggest downloading websites are not able to do so. You can follow their approach of redirecting to a new html page after clicking on Download File, then the new displayed page will state: "Your download is going to start after X no. of seconds." There the HTML refresh code will work by refreshing after a particular no. of seconds and redirect to the download.asp (which holds the actual code to download).

<meta http-equiv="refresh" content="5;URL=download.asp">

I think it will help you...

Best of Luck..

Khurram
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 7

Expert Comment

by:TheKenman
ID: 10782058
That makes sense, but I would still expect the refresh (or transfer) to occur once the download finished.. unless a download is automatically considered the end of the response object.

*Long-shot possibility:

Does anyone know if it'd be possible to use client-side FSO to programmatically save the file, possibly if used in conjunction with client-side xmlHTTP? If so, then you could simply set a window.setTimeout() routine (or use the native xmlHTTP response statuses) to check if the target file exists on the client, or if the target file on the client matches the filesize of the download file. This would of course require ActiveX warnings, but you could do something like so:

JSP --> write the appropriate file path to an xmlHTTP script on the client --> xmlHTTP download the file --> FSO to save the file to a directory of the user's choice. If successful, xmlHTTP --> server 'success' --> JSP to delete file --> now that the download has be programmatically monitored, it should be possible to use the refresh method of your choice to refresh the client page.

*More practical possibility:

A totally different (yet less elegant) alternative might be to open a pop-up (or simply another window) for the download, and at the same time refresh the original window. The new window would have 2 buttons, one that says "click here if download successful" (when clicked would refresh the original page and close the new window at the same time), and the 2nd button would be "download failed, try again" (which would just re-run the process).


Are you doing this for a webmail-type app, or....? Possibly if we knew the intended purpose we could better propose alternatives.
0
 
LVL 2

Expert Comment

by:khurram007
ID: 10782159
Well... what will you do if the user don't bother to click button and instead press ALT+F4 or X of windows button??? Well, what you r suggesting is possible but it is not a professional way to do it. I will certainly wait for the required solution(if possible) but the way I have suggested is being used even by the top most site for downloads, i.e, www.download.com. So, hope for the best.

Best of Luck,

Khurram
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 10782636
Respectfully Khurram, I think you're misunderstanding the dilemna.

He wishes to refresh/transfer AFTER the download is complete. AFAIK (as far as I know), download.com doesn't do that... it will send you to a pre-download page, which will say "click here if download doesn't start", and it will indeed use a meta refresh to start the download.....but what happens when the download finishes? Nothing. That's what he's trying to get past.
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 10782641
>> Well... what will you do if the user don't bother to click button and instead press ALT+F4 or X of windows button???

Use onBeforeUnLoad or onUnLoad, easy.
0
 

Author Comment

by:GEJames
ID: 10785955
Allow me to clarify:

The page shows a list of available files.  When the user downloads a file it will no longer be available to the user - i.e., I don't want the user to download the same file twice.  Therefore I want the page to refresh after the user has clicked the download button.  It doesn't matter to me if the refresh happens before, during, or after the actual download process, so long as it refreshes.

I thought about using an ActiveX control to manage downloads instead of using Response.WriteFile, but it doesn't seem as though there's much support for ActiveX in ASP.NET (perhaps I am mistaken).  Do you know if I could write a control that would accomplish this for me?  If I could enable multiple downloads that would be even better...

This site is on an intranet so there are no concerns if the user has to install .NET Framework or something like that, I just want to get this to work sometime soon.

Thanks

James
0
 
LVL 2

Expert Comment

by:khurram007
ID: 10847957
No answers... Can anyone provide the solution by any means??? Whatever the question is, it will be great if anyone can solve the solution...

Khurram
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 11021024
@GEJames: Sorry for the delay. I collaborate on several questions at a time and i guess this was lost among the 100's of Q's.

Getting to the point. How do you display the files available for download?. from a db?. Do you have a users table against which the users are authenticated?. If so, create another table (user_downloads) for tracking downloads. Whenever a user clicks on download, a record is updated in the user_downloads table corresponding to the file. The page is refreshed/ redirected back to the downloads page. The query would be something like

"Select * from tbl_downloads where download_id not in (select user_download_file_id from user downloads where user_download_user_id="&session("userID")&")"

Thus it will display only the files that the user has not downloaded.

Lemme know if you need more help with this.

Cheers!!
0
 

Expert Comment

by:Frilbz
ID: 11700287
I think this is what you are after. What I was wanting was the page to refresh after doing .writefile, but it wont writefile so you cant do a .redirect to refresh the page, so I did this:

When the dialog box to download pops up, they will hit save, then click back to the page, and onfocus this function will refresh the page:

<HEAD>
    <title>InfoOnline</title>
    <script language="javascript">
      function RefreshPage() {window.onfocus = PageToRedirectTo; window.blur; }
      function PageToRedirectTo() { self.location.href="/path/script_name.aspx"; }
    </script>
</HEAD>
  <body onfocus="RefreshPage()">
  When any link/button in here is clicked, a dialog box pops up then when that dialog box closes it will come back to this page and refresh it.
 </body>

</html>

Hope this helps :)
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12734523
PAQed with no points refunded (of 500)

modulo
Community Support Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

11 Experts available now in Live!

Get 1:1 Help Now