gpt4 book ai didi

asp.net - 无法将类型'System.Web.HttpPostedFile'隐式转换为'System.Web.HttpPostedFileBase'

转载 作者:行者123 更新时间:2023-12-03 21:19:35 24 4
gpt4 key购买 nike

我有这种方法不能建立,它与消息错误:


无法将类型'System.Web.HttpPostedFile'隐式转换为'System.Web.HttpPostedFileBase'


我真的需要它的类型是HttpPostedFileBase而不是HttpPostedFile,我尝试了装箱,但它不起作用:

foreach (string inputTagName in HttpContext.Current.Request.Files)
{
HttpPostedFileBase filebase =HttpContext.Current.Request.Files[inputTagName];
if (filebase.ContentLength > 0)
{
if (filebase.ContentType.Contains("image/"))
{
SaveNonAutoExtractedThumbnails(doc, filebase);
}
}
}

最佳答案

快速浏览Reflector表示HttpPostedFileWrapperHttpPostedFileBase继承并在构造函数中接受HttpPostedFile

foreach (string inputTagName in HttpContext.Current.Request.Files)
{
HttpPostedFileBase filebase =
new HttpPostedFileWrapper(HttpContext.Current.Request.Files[inputTagName]);

if (filebase.ContentLength > 0)
{
//...


TheVillageIdiot提出了一个关于更好的循环结构的要点,如果您的作用域公开了当前HTTP上下文的 Request属性(例如,在 Page上,但不在 Global.asax中),它将对您有用:

foreach (HttpPostedFile file in Request.Files)
{
HttpPostedFileBase filebase = new HttpPostedFileWrapper(file);
// ..


如果您有可用的LINQ,也可以使用它:

var files = Request.Files.Cast<HttpPostedFile>()
.Select(file => new HttpPostedFileWrapper(file))
.Where(file => file.ContentLength > 0
&& file.ContentType.StartsWith("image/"));

foreach (var file in files)
{
SaveNonAutoExtractedThumbnails(doc, file);
}

关于asp.net - 无法将类型'System.Web.HttpPostedFile'隐式转换为'System.Web.HttpPostedFileBase',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3071902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com