Pinning files and loading them using the SaveAs dialog

I have the following code to zip all files and then save it to my hard drive. I want to zip all files (this is done) and then attach the zip file to the Response stream so that the user can save it!

protected void DownloadSelectedFiles_Click(object sender, EventArgs e)
        {
            string path = String.Empty;
            string zipFilePath = @"C:\MyZipFile.zip";

            ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath));
            byte[] buffer = new byte[4096];

            foreach (GridViewRow row in gvFiles.Rows)
            {
                bool isSelected = (row.FindControl("chkSelect") as CheckBox).Checked;

                if (isSelected)
                {
                    path = (row.FindControl("lblUrl") as Label).Text;

                    ZipEntry entry = new ZipEntry(Path.GetFileName(path));
                    entry.DateTime = DateTime.Now;

                    zipStream.PutNextEntry(entry);

                    using (FileStream fs = File.OpenRead(path))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            zipStream.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
            }


            zipStream.Finish();
            zipStream.Close();

            Response.ContentType = "application/x-zip-compressed";
            Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");
            Response.WriteFile(zipFilePath);
            Response.End();

        }

      

+1


source to share


5 answers


I wrote about submitting files like this a while ago. You might find something useful there.



http://absolutecobblers.blogspot.com/2008/02/downloading-and-deleting-temporary.html

+1


source


If you're using IE, make sure it's not the old "cache full" error showing its ugly face.

And if you have IE installed to automatically refresh the cache, or when you start IE and downloaded to that zip file for the first time, it might be that it is using the cached version of that zip file after you fixed your procedure and got a nice zip index.

Try adding:

  Response.Buffer = true;
  Response.Clear();
  Response.ClearContent();
  Response.ClearHeaders();

      

before the .ContentType answer and add this:



Response.Flush()
Response.Close()

      

before the .end answer, and see if that changes anything.

So the result is this:

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/x-zip-compressed";  
Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");  
Response.WriteFile(zipFilePath);  
Response.Flush()
Response.Close()
Response.End();  

      

Just a few tips only from the top of the mind.

+1


source


And long shot, try setting mime type to app / unknown, this post seems to have part of the solution to the posters issue:

http://jamesewelch.com/2008/12/03/sharpziplib-and-windows-extraction-wizard-errors/

0


source


I also tried to save the zip file to the server folder and then gave a link to the user to download it, but he says the same thing: "The file is corrupted." This is very strange since I can open the same folder if I am in my server folder and open it manually!

0


source


Here is another thing I found. If I save the zip file in the Server folder and then link to it with the url: http: //localhost/MyApplication/ServerFolder/MyZipFile.zip .

If I go to the url and download the zip file, I get the same "File corrupted" error. But if I manually navigate to the folder using file explorer and open the file, it works as expected.

Why and how?

0


source







All Articles