HttpFileCollection 中的无效转换异常

发布于 2024-08-15 16:24:43 字数 1489 浏览 10 评论 0原文

我下面有一个扩展方法,但是当我运行它时,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 内容类型的 MIME 格式 多部分/表单数据的标头。网络平台 从中提取编码文件 内容主体分解为个人成员 HttpFileCollection 的。方法和 HttpPostedFile 类的属性 提供对内容的访问和 每个文件的属性。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的那么颓废 2024-08-22 16:24:43

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

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

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

薔薇婲 2024-08-22 16:24:43

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

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

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...
}
我不在是我 2024-08-22 16:24:43

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

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

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;
                }
伤痕我心 2024-08-22 16:24:43
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;
    }
  }
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;
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文