首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我在迭代HttpPostedFile时得到的是HttpPostedFileBase而不是HttpPostedFileBase?

为什么我在迭代HttpPostedFile时得到的是HttpPostedFileBase而不是HttpPostedFileBase?
EN

Stack Overflow用户
提问于 2022-07-02 18:16:34
回答 1查看 159关注 0票数 0

我正在迭代一个HttpFileCollection,并试图得到一个列表作为结果。

代码语言:javascript
复制
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中获得列表?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-02 18:30:52

HttpPostedFile不是从HttpPostedFileBase派生的。如果您真的想返回List而不是List,那么将每个HttpPostedFile对象包装在HttpPostedFileWrapper对象中:

代码语言:javascript
复制
HttpPostedFileBase castedFile = new HttpPostedFileWrapper(files[fileName]);

正如HttpPostedFileWrapper文档所说:

HttpPostedFileWrapper类派生自HttpPostedFileBase类,并充当HttpPostedFile类的包装器。这个类公开了HttpPostedFile类的功能,同时也公开了HttpPostedFileBase类型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72841439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档