views:

1904

answers:

3

Hi ,

i got a download page , there 3 download option , Word , Zip , PDF .. and there is a folder in there so many .DOC file. I want when a user click ZIP option in page , ASP.NET zip this folder with doc files than it will be temp zip file , and then client will save it from server.When user's download is finished then tempZip file will delete itself... this is our scenario ?

How can i do this scenario with ASP.NET 2.0 C# .. ??

Note : I know how can i zip and unzip files and remove files from system with C# asp.net 2.0 .

A: 

You'll want to stream the zip file to the user manually, then delete the file when streaming is complete.

try
{
    Response.WriteFile( "path to .zip" );
}
finally
{
    File.Delete( "path to .zip" );
}
Paul Alexander
while response.write in action client send 2 request Post and Get to server . in first action its enter try block bla bla bla than enter Finally block and remove file .. and fire event again and try to download file but there is tempziP file cuz its removed in first request... SOO How can i solve this problem ?
Ibrahim AKGUN
A: 

i fixed my problem with adding to end of the stream code,

Response.Flush();
Response.Close();
if(File.Exist(tempFile))
{File.Delete(tempFile)};

Thats solves my problem.. thanx all.

Ibrahim AKGUN
+2  A: 

Using DotNetZip you can save the zip file directly to the Response.OutputStream. No need for a temporary Zip file.

    Response.Clear();
    // no buffering - allows large zip files to download as they are zipped
    Response.BufferOutput = false;
    String ReadmeText= "Dynamic content for a readme file...\n" + 
                       DateTime.Now.ToString("G");
    string archiveName= String.Format("archive-{0}.zip", 
                                      DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "attachment; filename=" + archiveName);
    using (ZipFile zip = new ZipFile())
    {
        // add a file entry into the zip, using content from a string
        zip.AddFileFromString("Readme.txt", "", ReadmeText);
        // add the set of files to the zip
        zip.AddFiles(filesToInclude, "files");
        // compress and write the output to OutputStream
        zip.Save(Response.OutputStream);
    }
    Response.Close();
Cheeso