0

I have a web interface where users can choose one of many files from local computer and upload them to a central location, in this case Azure Blob Storage. I have a check in my C# code to validate that the filename ending is .bin. The receiving method in C# takes an array of HttpPostedFileBase.

I want to allow users to choose a zipfile instead. In my C# code, I iterate through the content of the zipfile and check each filename to verify that the ending is .bin.

However, when I iterate through the zipfile, the ContentLength of the HttpPostedFileBase object becomes 0 (zero) and when I later on upload the zipfile to Azure, it is empty.

How can I make a check for filename endings without manipulating the zipfile?

  • I have tried to DeepCopy a single object of HttpPostedFileBase but it is not serializable.
  • I've tried to make a copy of the array but nothing works. It seems that everything is reference and not value. Some example of my code as follows. Yes, I tried the lines individually.

private static bool CanUploadBatchOfFiles(HttpPostedFileBase[] files)
{
    var filesCopy = new HttpPostedFileBase[files.Length];
    // Neither of these lines works
    Array.Copy(files, 0, filesCopy, 0, files.Length);
    Array.Copy(files, filesCopy, files.Length);
    files.CopyTo(filesCopy, 0);
}

This is how I iterate through the zipfile

foreach (var file in filesCopy)
{
    if (file.FileName.EndsWith(".zip"))
    {
        using (ZipArchive zipFile = new ZipArchive(file.InputStream))
        {
            foreach (ZipArchiveEntry entry in zipFile.Entries)
            {
                if (entry.Name.EndsWith(".bin"))
                {
                    // Some code left out
                }
            }
        }
    }
}
CSDev
  • 3,177
  • 6
  • 19
  • 37
Martin Nilsson
  • 459
  • 1
  • 5
  • 15
  • Are you sure your files have the data before iteration? – Nadeem Jul 30 '19 at 09:47
  • Yes. If I skip the iteration and just upload the zipfile as is, it works and a zipfile containing two .bin files with data is stored in Azure Blob Storage. If I hover the 'files' object in my CanUploadBatchOfFiles method, I see that the zipfile has ContentLength 27484 before iteration but 0 after. – Martin Nilsson Jul 30 '19 at 09:50
  • Just to check are you iterating through files or filescopy – Nadeem Jul 30 '19 at 12:46
  • I would suggest you use for loop instead of ForEach and check – Nadeem Jul 30 '19 at 12:57
  • I'm iteration through my filescopy. The change from foreach to for loop does not work. The first problem is that Copy function of the array only copies by reference and not by value. This makes the copy useless. The second problem is that the zip file becomes extracted or something, the content length becomes zero after iterating. The third problem is that I cant do a deep copy of the HttpPostedFileBase class since it not market as serializable, which I found as a solution. – Martin Nilsson Jul 31 '19 at 07:11
  • So, I have (at least) three different approaches, if any of them can be solved, that would probably be my solution. However, I can't find any good solution to any of the problems. – Martin Nilsson Jul 31 '19 at 07:15

1 Answers1

1

I solved my problem. I had to do two separate things:

First, I do not do a copy of the array. Instead, for each zip file, I just copy the stream. This made the ContentLength stay at whatever length it was.

The second thing is did was to reset the position after I looked inside the zipfile. I need to do this or else the zip file that I upload to Azure Blob Storage will be empty.

private static bool CanUploadBatchOfFiles(HttpPostedFileBase[] files)
{
    foreach (var file in files)
    {
        if (file.FileName.EndsWith(".zip"))
        {
            // Part one of the solution
            Stream fileCopy = new MemoryStream();
            file.InputStream.CopyTo(fileCopy);

            using (ZipArchive zipFile = new ZipArchive(fileCopy))
            {
                foreach (ZipArchiveEntry entry in zipFile.Entries)
                {
                    // Code left out
                }
            }

            // Part two of the solution
            file.InputStream.Position = 0;
        }
    }

    return true;
}
Martin Nilsson
  • 459
  • 1
  • 5
  • 15