Cannot implicitly convert type 'System.Web.HttpPostedFile' to 'System.Web.HttpPostedFileBase'

来源:互联网 发布:福田电动三轮车淘宝 编辑:程序博客网 时间:2024/05/22 17:30

http://stackoverflow.com/questions/3071902/cannot-implicitly-convert-type-system-web-httppostedfile-to-system-web-httppo

A quick peek at Reflector indicates that HttpPostedFileWrapper inherits from HttpPostedFileBase and accepts an HttpPostedFile in the constructor:

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

TheVillageIdiot brings up a great point about the better looping construct, and it will work for you if you're scope exposes the Request property of the current HTTP context (e.g. on a Page, but not in Global.asax):

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

If you have LINQ available, you could use that as well:

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);}
0 0
原创粉丝点击