views:

343

answers:

3

Hi Everyone,

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.

+3  A: 

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
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
A: 

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;
                }
Braveyard
+1  A: 

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