none
System.Web.HttpFileCollectionWrapper - An odd beast - Enumerator exhibits weird behavior, not conventional, opinions? RRS feed

  • Question

  • The "System.Web.HttpFileCollectionWrapper" .NET Framework type implements System.Collections.IEnumerable so I've tried enumerating over an instance of it using a foreach() construct as follows:

    foreach(var X in this.Request.Files) {

        // do something with object in collection

    }

    This enumerator returns just a key (System.String) in the collection and if a user sends multiple files using a single HTML input element of type file, all the keys are the same.

    However, the indexer returns the appropriate objects one would expect to be getting from the enumerator. Which is a System.Web.HttpPostedFileWrapper for each file sent up by the user.

    Does anyone besides myself think this is just weird? Obviously, it can be done programatically but from an intuitive perspective about what a programmer expects to get back when enumerating through the type I can't help but be baffled by the behavior personally.

    Thoughts please? Thanks in advance!

    Monday, October 22, 2018 9:31 PM

Answers

  • You get the reason why the keys can be the same (because anything returned by the form is "indexed" by the name attribute of the input controls, and name attribute does not need to be unique - if more than 1 input control has the same name, then the request handler will group them into array.

    Maybe it's less intutive from people who don't know a little bit of history of what the web has been like before, but it's very intutive to those who had worked with ASP or other web technologies since the days of HTML4. And believe me it's more handy this way if you have a dynamic web page that has "Add" button to allow people upload arbitrary number of files for different purpose. It's way easier to write code handling the files in this way.


    Tuesday, October 23, 2018 3:55 AM
    Answerer

All replies

  • Hi 7h3D4rkKn1gh7,

    Thank you for posting here.

    For your question, you could try the code below. 

    foreach (string fileName in Request.Files)
    {
        HttpPostedFileBase file = Request.Files[fileName];
    
        ...
    }

    For more details, please refer to the thread in StackOverFlow.

    https://stackoverflow.com/questions/1760510/foreach-on-request-files

    You could check the examples in the MSDN document as well.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.files?view=netframework-4.7.2

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, October 23, 2018 1:47 AM
    Moderator
  • Hi Wendy,

    Thank you very much for your post. Those are links to some good solutions for people having issues with this type. However, my question was more theoretical and maybe a bit philosophical versus looking for an actual solution.

    I'm questioning why the enumerator of the type in question returns a string key in a foreach loop. And why it returns the same key everytime, no matter how many files were uploaded the user?

    The type seems to accommodate the possibility of multiple HTML element inputs of type set equal to "file". It's just weird to me still.



    Tuesday, October 23, 2018 3:10 AM
  • You get the reason why the keys can be the same (because anything returned by the form is "indexed" by the name attribute of the input controls, and name attribute does not need to be unique - if more than 1 input control has the same name, then the request handler will group them into array.

    Maybe it's less intutive from people who don't know a little bit of history of what the web has been like before, but it's very intutive to those who had worked with ASP or other web technologies since the days of HTML4. And believe me it's more handy this way if you have a dynamic web page that has "Add" button to allow people upload arbitrary number of files for different purpose. It's way easier to write code handling the files in this way.


    Tuesday, October 23, 2018 3:55 AM
    Answerer
  • Thank you very much for your post. Your answer shows that knowing the past is important in understanding the way things are in the present. And that's what I was looking for.

    I could begin to see the rationale behind the implementation when I thought more about it as I was hinting to in the last paragraph of my last post. And you have confirmed it.

    Thanks again!

    Tuesday, October 23, 2018 3:32 PM