Reputation: 135
I have created a mdb file,and zipping that file using asp.net and after that I am downloading the zip file.
After downloading file I just want to delete the file from the directory by using c#?
I just tried but the downloading is done after the button click exist but I want to download the file and after downloading it is deleted from the directory.
Upvotes: 1
Views: 1655
Reputation: 3110
Have you tried this code?
string filename = "yourfilename";
if (filename != "")
{
string path = Server.MapPath(filename);
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
//Content-Disposition will tell the browser how to treat the file.(e.g. in case of jpg file, Either to display the file in browser or download it)
//Here the attachement is important. which is telling the browser to output as an attachment and the name that is to be displayed on the download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
//Telling length of the content..
Response.AddHeader("Content-Length", file.Length.ToString());
//Type of the file, whether it is exe, pdf, jpeg etc etc
Response.ContentType = "application/octet-stream";
//Writing the content of the file in response to send back to client..
Response.WriteFile(file.FullName);
Response.End();
// Delete the file...
System.IO.File.Delete(file.FullName);
}
else
{
Response.Write("This file does not exist.");
}
}
Upvotes: 1
Reputation: 311
Have a example:
protected virtual void DownloadNDelete(string sFilePath, string sContentType)
{
string FileName = Path.GetFileName(sFilePath);
Response.Clear();
Response.AddHeader("Content-Disposition","attachment; filename=" + FileName);
Response.ContentType = sContentType;
Response.WriteFile(sFilePath);
Response.Flush();
this.DeleteFile(sFilePath);
Response.End();
}
Upvotes: 3
Reputation: 446
This may help you
String filename=Directory.GetFile(@"c:\filename");
File.Delete(filename);
Upvotes: 1