HttpFileCollection 中的无效转换异常
我下面有一个扩展方法,但是当我运行它时,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您查看此页面上的代码示例,它会显示您应该如何枚举该集合,当您尝试按原样枚举时,实际上您会得到一个字符串。
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
HttpFileCollection
集合枚举器返回键。您需要在循环的每次迭代中使用键来查找关联的HttpPostedFile
对象。所以你的循环需要如下所示:The
HttpFileCollection
collection enumerator returns keys. You need to use the key in each iteration of the loop to look up the associatedHttpPostedFile
object. So your loop needs to look like this:好吧,我找到了一个解决方案,但它看起来很愚蠢,但它有效。
我只是用这个更改了
foreach
:Well I've found a solution but it looks so stupid but it works.
I've simply changed the
foreach
with this one :