Solved

how do you execute code after the user downloads a file?

Posted on 2006-07-12
6
845 Views
Last Modified: 2008-02-01
Hello,

I have code that prompts the user to download a file (a word document).  I am using asp.net 1.1 C#

My problem is, that after the user downloads the file, the request ends.  Nothing else will execute on the page.  For example:

 Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + strOriginalFileName + "." + strFileToUploadExtension);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();

                string newString;
                System.IO.File.Delete(strFilePath);

the File.Delete()...

will not execute after the user downloads teh file.


Does anyone know how to fix this?

Thanks
0
Comment
Question by:alexthecodepoet
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 17095824
As soon as you issue the Response.End command you cannot send back anything else ass you have told the browser the response is complete.

I think you are going to be stuck doing that if you are streaming content.

You could try issuing the command right before the Response.End, but I don't knwo what it will do to delete the file before the response has ended.
0
 

Author Comment

by:alexthecodepoet
ID: 17095898
i see,

so then, is it possible to keep running code?  or can you think of any alternatives?

Thanks,

0
 
LVL 35

Expert Comment

by:mrichmon
ID: 17095922
I don't understand what you mean by "keep code running"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:alexthecodepoet
ID: 17095983
once i call the code that lets the user download the file, i need to keep running code..
that is, when i do this:

Response.WriteFile(file.FullName);
Response.End();

I then need to delete the file.

But the page stops running or something after i do Response.End();  so how do i go about executing code after i do Response.End();

?

Thanks
0
 
LVL 5

Accepted Solution

by:
Jojo1771 earned 500 total points
ID: 17095987
Simply move your   Response.End(); to the end of the page event prerender.  I'm guessing

If I follow you. You want the user to dl a file then run some other code. By moving the   Response.End(); to the  end of prerender you should be able to 1 write it out to the buffer. Then delete the file as its byte array is now in the buffer then in prerender send the   Response.End(); So the page never accatully renders. The user won't see anything until you issure the   Response.End(); But lets face it, I think we are talking nano seconds.

Thanks,
Jojo1771
0
 

Author Comment

by:alexthecodepoet
ID: 17096177
It turns out that you can just remove the Response.End alltogether and everythign works out dandy :)

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

13 Experts available now in Live!

Get 1:1 Help Now