我正在迭代一个HttpFileCollection,并试图得到一个列表作为结果。
public List<HttpPostedFileBase> GetFiles()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
List<HttpPostedFileBase> result = new List<HttpPostedFileBase>();
foreach (string fileName in files)
{
HttpPostedFileBase castedFile = files[fileName]; //This is HttpPostedFile and not HttpPostedFileBase
result.Add(castedFile);
}
return result;
}如何从HttpFileCollection中获得列表?
发布于 2022-07-02 18:30:52
HttpPostedFile不是从HttpPostedFileBase派生的。如果您真的想返回List而不是List,那么将每个HttpPostedFile对象包装在HttpPostedFileWrapper对象中:
HttpPostedFileBase castedFile = new HttpPostedFileWrapper(files[fileName]);HttpPostedFileWrapper类派生自HttpPostedFileBase类,并充当HttpPostedFile类的包装器。这个类公开了HttpPostedFile类的功能,同时也公开了HttpPostedFileBase类型。
https://stackoverflow.com/questions/72841439
复制相似问题