I am trying to download zip files on the basis of multiple selection meaning the user selects documents and presses the download files button and then the zip file is generated.
Everything is working correctly. My zip files are also being downloaded. But sometimes when I press download button again and again , it give me the below error. I have noticed that this error is not generated when I download any new files. But when I download those files which I have already downloaded mutiiple times, then this error is generated
An item with the same key has already been added.
Note this error is generated very rare. And I cant seem to figure out why after multiple google searches. I am posting my code below. Can anyone help me?
using (ZipFile zip = new ZipFile())
{
foreach (DataRow row in dt.Rows)
{
//some code
zip.AddFile(filePath, "files"); //here the error is
generated
}
Response.Clear();
//Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
Response.ContentType = "application/zip";
zip.Save(Response.OutputStream);
Response.End();
Adding the current time to a file name will not insure uniqueness if you process two of them during the same one-second interval.
Try replacing
zip.AddFile(filePath, "files");
with this code:This alters the file name in the same way that Windows does when you try to copy files into a folder where the file names already exist.
Then you won't need to include the time in the file name to insure uniqueness.