Tag: httppostedfilebase

当我上传0个文件时,为什么IEnumerable count为1?

我有一个多上传表单,我想在启动上传时检查是否有任何文件。 这是我的代码。 查看: @using (Html.BeginForm(“Upload”, “Home”, FormMethod.Post, new { enctype = “multipart/form-data”})) { } 控制器: [HttpPost] public ActionResult Upload(IEnumerable files) { if (files.Count() > 0) Console.WriteLine(files.Count()); // display 1 if(files.Any()) Console.WriteLine(files.Any()); // display true if (files.First() == null) Console.WriteLine(“first null”); // display “first null” return View(); } 当我提交空表单时,为什么我的程序会显示结果? 我可能会检查JS我的字段,但我想了解我的IEnumerable这些数据是什么。 谢谢。

MVC3如何检查HttpPostedFileBase是否为图像

我有一个像这样的控制器: public ActionResult Upload (int id, HttpPostedFileBase uploadFile) { …. } 如何确保uploadFile是一个图像(jpg,png等) 我试过了 using (var bitmapImage = new Bitmap (uploadFile.InputStream)) {..} 如果无法创建bitmapImage,则会抛出ArgumentException。 有没有更好的方法,例如通过查看up​​loadFile.FileName?

ASP MVC文件上传HttpPostedFileBase是空的

在我的控制器中,因为我希望能够填写有关video的一些细节并实际上传它,所以Video类不需要实际的video,因为它将被传递给另一个Web服务。 public class VideoUploadModel { public HttpPostedFileBase vid { get; set; } public Video videoModel { get; set; } } // // POST: /Video/Create [HttpPost] public ActionResult Create(VideoUploadModel VM) { if (ModelState.IsValid) { db.Videos.AddObject(VM.videoModel); db.SaveChanges(); return RedirectToAction(“Index”); } ViewBag.UserId = new SelectList(db.DBUsers, “Id”, “FName”, VM.videoModel.UserId); return View(VM); } 在我看来,我有 @model LifeHighlightsShoeLace.Controllers.VideoController.VideoUploadModel @{ ViewBag.Title = “Create”; […]

HttpPostedFileBase与HttpPostedFileWrapper的关系

我理解HttpPostedFileBase和HttpPostedFileWrapper之间的关系,就两者的需求而言(即在unit testing/模拟中)。 但是,为什么当我在HttpPostedFileBase的返回上设置断点时,它是否将它显示为HttpPostedFileWrapper ? 此外, HttpPostedFileBase不实现ContentType属性。 那么为什么当我的代码只引用HttpPostedFileBase而不是HttpPostedFileWrapper时它返回一个值? 这是什么样的诡计? 编辑#1: 感谢@ lawliet29的精彩回复。 我按照建议书写了结构。 public sealed class MyHttpPostedFile { public string ContentType { get { return “123”; } } } public abstract class MyHttpPostedFileBase { } public class MyHttpPostedFileWrapper : MyHttpPostedFileBase { private MyHttpPostedFile _myHttpPostedFile; public MyHttpPostedFileWrapper(MyHttpPostedFile myHttpPostedFile) { _myHttpPostedFile = myHttpPostedFile; } public string ContentType { […]