Tar*_*rik 3 c# asp.net httppostedfile 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;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
MSDN说:
客户端使用多部分MIME格式编码文件并在内容正文中传输它们,并使用multipart/form-data的HTTP Content-Type标头.ASP.NET将编码文件从内容主体提取到HttpFileCollection的各个成员中.HttpPostedFile类的方法和属性提供对每个文件的内容和属性的访问.
如果你查看这个页面上的代码示例,它会显示你应该如何枚举集合,实际上当你尝试枚举时,你会得到一个字符串.
http://msdn.microsoft.com/en-us/library/system.web.httpfilecollection.aspx
| 归档时间: |
|
| 查看次数: |
6923 次 |
| 最近记录: |