ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
ASP.Net - How to delete file from server after download is finished
Author Name: Mudassar Khan Published Date: October 08, 2009
Filed Under :
ASP.Net
Views: 3427

Here I am providing the code snippet that will allow us to delete the file once the file is downloaded on the client’s machine. Below are the code snippets

C#

private void DownloadFile()

{

    Response.ContentType = ContentType;

    Response.AppendHeader("Content-Disposition",

                    "attachment; filename=myFile.txt");

    Response.WriteFile(Server.MapPath("~/uploads/myFile.txt"));

    Response.Flush();

    System.IO.File.Delete(Server.MapPath("~/uploads/myFile.txt"));

    Response.End();

}

 

VB.Net

Private Sub DownloadFile()

        Response.ContentType = ContentType

        Response.AppendHeader("Content-Disposition", _

                              "attachment; filename=myFile.txt")

        Response.WriteFile(Server.MapPath("~/uploads/myFile.txt"))

        Response.Flush()

        System.IO.File.Delete(Server.MapPath("~/uploads/myFile.txt"))

        Response.End()

End Sub

 

That’s it. Try it and let me know in case any issues or queries.


 

If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code