3

I have an extension method below, but when I run this, the foreach gives me InvalidCastException and it says *

Unable to cast object of type 'System.String' to type 'System.Web.HttpPostedFile'.

Code :

public static List<Attachment> GetFiles(this HttpFileCollection collection) {
            if (collection.Count > 0) {
                List<Attachment> items = new List<Attachment>();
                foreach (HttpPostedFile _file in collection) {
                    if (_file.ContentLength > 0)
                        items.Add(new Attachment()
                        {
                            ContentType = _file.ContentType,
                            Name = _file.FileName.LastIndexOf('\\') > 0 ? _file.FileName.Substring(_file.FileName.LastIndexOf('\\') + 1) : _file.FileName,
                            Size = _file.ContentLength / 1024,
                            FileContent = new Binary(new BinaryReader(_file.InputStream).ReadBytes((int)_file.InputStream.Length))
                        });

                    else
                        continue;
                }
                return items;
            } else
                return null;
        }

Thanks in advance.

MSDN Says :

Clients encode files and transmit them in the content body using multipart MIME format with an HTTP Content-Type header of multipart/form-data. ASP.NET extracts the encoded file(s) from the content body into individual members of an HttpFileCollection. Methods and properties of the HttpPostedFile class provide access to the contents and properties of each file.

Tarik
  • 79,711
  • 83
  • 236
  • 349

4 Answers4

7

If you look at the code sample on this page, it shows how you should enumerate the collection, you are in fact getting a string when you try to enumerate as you are.

http://msdn.microsoft.com/en-us/library/system.web.httpfilecollection.aspx

Jarrett Widman
  • 6,329
  • 4
  • 23
  • 32
  • Yes. The string you are getting back is the name of the input element corresponding to the file. If you want, you can just do a for (int i = 0; i < collection.Count; i++) { HttpPostedFile file = collection[i]; /* remainder of code here */ }. – Levi Dec 15 '09 at 06:57
3

The HttpFileCollection collection enumerator returns keys. You need to use the key in each iteration of the loop to look up the associated HttpPostedFile object. So your loop needs to look like this:

foreach (string name in collection) {
    HttpPostedFile _file = collection[name];
    // ...rest of your loop code...
}
Atif Aziz
  • 36,108
  • 16
  • 64
  • 74
1
HttpFileCollection hfc = Request.Files;
  for (int i = 0; i < hfc.Count; i++)
  {
     HttpPostedFile hpf = hfc[i];
     if (hpf.ContentLength > 0)
    {
     string _fileSavePath = _DocPhysicalPath  + "_" + hpf.FileName;
    }
  }
  • Put the HtpCollection Request file that will be having an collection of array's into httpposted file. It will help if there are more than one file uploader on a page to find out which one is uploaded on a page. – Robin Kumar Jan 31 '13 at 13:46
1

Well I've found a solution but it looks so stupid but it works.

I've simply changed the foreach with this one :

foreach (string fileString in collection.AllKeys) {
                    HttpPostedFile _file = collection[fileString];
                    if (_file.ContentLength > 0)

                        items.Add(new Attachment()
                        {
                            ContentType = _file.ContentType,
                            Name = _file.FileName.LastIndexOf('\\') > 0 ? _file.FileName.Substring(_file.FileName.LastIndexOf('\\') + 1) : _file.FileName,
                            Size = _file.ContentLength / 1024,
                            FileContent = new Binary(new BinaryReader(_file.InputStream).ReadBytes((int)_file.InputStream.Length))
                        });

                    else
                        continue;
                }
Tarik
  • 79,711
  • 83
  • 236
  • 349