当迭代httpfilecollection时,为什么要获得httppostedfile而不是httppostedfilebase?

发布于 2025-02-13 00:52:11 字数 578 浏览 1 评论 0原文

我在httpfilecollection上迭代,并尝试获取列表< httppostedfilebase>结果。

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;
}

我如何获得列表&lt; httppostedfilebase&gt;从httpfilecollection中?

I'm iterating over an HttpFileCollection and trying to get a List<HttpPostedFileBase> as the result.

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;
}

How can I get a List<HttpPostedFileBase> out of an HttpFileCollection?

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

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

发布评论

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

评论(1

寂寞美少年 2025-02-20 00:52:11

httppostedfile 不是源自httppostedfilebase。如果您真的想返回列表&lt; httppostedfilebase&gt; instead of List, then wrap each HttpPostedFile object within an HttpPostedFileWrapper object:

HttpPostedFileBase castedFile = new HttpPostedFileWrapper(files[fileName]);

As the httppostedfilewrapper文档说:

httppostedfilewrapper类来自HttppostedFilebase类,并用作HttppostedFile类的包装器。此类公开了HTTPPOSTEDFILE类的功能,同时还展示了HttppostedFilebase类型。

HttpPostedFile does not derive from HttpPostedFileBase. If you really want to return List<HttpPostedFileBase> instead of List<HttpPostedFile>, then wrap each HttpPostedFile object within an HttpPostedFileWrapper object:

HttpPostedFileBase castedFile = new HttpPostedFileWrapper(files[fileName]);

As the HttpPostedFileWrapper documentation says:

The HttpPostedFileWrapper class derives from the HttpPostedFileBase class and serves as a wrapper for the HttpPostedFile class. This class exposes the functionality of the HttpPostedFile class while also exposing the HttpPostedFileBase type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文