【问题标题】:Invalid Cast Exception in HttpFileCollectionHttpFileCollection 中的无效转换异常
【发布时间】:2010-12-26 17:02:53
【问题描述】:

我在下面有一个扩展方法,但是当我运行它时,foreach 给了我InvalidCastException,它说 *

无法转换类型的对象 'System.String' 键入 'System.Web.HttpPostedFile'。

代码:

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

提前致谢。

MSDN 说:

客户端对文件进行编码并传输它们 在内容正文中使用多部分 带有 HTTP Content-Type 的 MIME 格式 多部分/表单数据的标题。 ASP.NET 从 内容主体转化为单个成员 一个 HttpFileCollection。方法和 HttpPostedFile 类的属性 提供对内容的访问和 每个文件的属性。

【问题讨论】:

  • 在给定的代码中在哪一行抛出此错误
  • @solairaja,当它第一次点击foreach (HttpPostedFile _file in collection) 代码行时,它给出了错误。

标签: c# asp.net httppostedfile httpfilecollection


【解决方案1】:

如果您查看此页面上的代码示例,它会显示您应该如何枚举集合,实际上当您尝试按原样枚举时会得到一个字符串。

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

【讨论】:

  • 是的。您返回的字符串是与文件对应的输入元素的名称。如果你愿意,你可以只做一个 for (int i = 0; i
【解决方案2】:

好吧,我找到了一个解决方案,但它看起来很愚蠢,但它确实有效。

我只是用这个更改了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;
                }

【讨论】:

    【解决方案3】:

    HttpFileCollection 集合枚举器返回键。您需要在循环的每次迭代中使用键来查找关联的HttpPostedFile 对象。所以你的循环需要看起来像这样:

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

    【讨论】:

      【解决方案4】:
      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;
          }
        }
      

      【讨论】:

      • 将包含数组集合的 HtpCollection 请求文件放入 httpposted 文件中。如果一个页面上有多个文件上传器,这将有助于找出一个页面上上传了哪个文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多