简体   繁体   English

HttpFileCollection中的无效Cast异常

[英]Invalid Cast Exception in HttpFileCollection

I have an extension method below, but when I run this, the foreach gives me InvalidCastException and it says * 我在下面有一个扩展方法,但是当我运行它时,foreach给了我InvalidCastException并且它说*

Unable to cast object of type 'System.String' to type 'System.Web.HttpPostedFile'. 无法将类型为“System.String”的对象强制转换为“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 : MSDN说:

Clients encode files and transmit them in the content body using multipart MIME format with an HTTP Content-Type header of multipart/form-data. 客户端使用多部分MIME格式编码文件并在内容正文中传输它们,并使用multipart / form-data的HTTP Content-Type标头。 ASP.NET extracts the encoded file(s) from the content body into individual members of an HttpFileCollection. ASP.NET将编码文件从内容主体提取到HttpFileCollection的各个成员中。 Methods and properties of the HttpPostedFile class provide access to the contents and properties of each file. HttpPostedFile类的方法和属性提供对每个文件的内容和属性的访问。

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 http://msdn.microsoft.com/en-us/library/system.web.httpfilecollection.aspx

The HttpFileCollection collection enumerator returns keys. HttpFileCollection集合枚举器返回键。 You need to use the key in each iteration of the loop to look up the associated HttpPostedFile object. 您需要在循环的每次迭代中使用键来查找关联的HttpPostedFile对象。 So your loop needs to look like this: 所以你的循环需要如下所示:

foreach (string name in collection) {
    HttpPostedFile _file = collection[name];
    // ...rest of your loop code...
}

Well I've found a solution but it looks so stupid but it works. 好吧,我找到了一个解决方案,但它看起来很愚蠢,但它的工作原理。

I've simply changed the foreach with this one : 我只是用这个改变了foreach

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;
                }
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;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM