我正在使用ASP.NET MVC,我有一个上传文件的动作.该文件正在正确上载.但我想要图像的宽度和高度.我想我需要将其转换HttpPostedFileBase
为Image
第一个然后继续.我怎么做?
如果有另一种更好的方法来获得图像的宽度和高度,请告诉我.
我正在尝试使用上传图片MVC 6
; 但是,我无法找到班级HttpPostedFileBase
.我检查过GitHub
,没有运气.有谁知道上传文件的正确方法MVC6
?
问题的背景.
编辑3 我可以验证这似乎在Chrome 20中再次运行.谢谢!
TLDR更新
编辑2 进一步捣乱后,这似乎是Chrome 19中的一个错误.请看这里:
没有什么比最新的bug了!:-)
Firefox-latest和IE8/9按预期工作.Fiddler日志显示相同的行为,主要区别在于第二次401授权检查不会发送表格有效负载乱码并且它按预期工作.
这听起来像一个测试用例,修复工作正在进行中!所以,希望对于那些可能遇到过这种情况的人来说,这会有所帮助!
结束TLDR
我有一个控制器操作,只接受一个上传的文件,这是一个"事件"的CSV文件.在幕后,此CSV被解析出来,变成了事件对象,数据在数据库中同步.结果输出时,系统会提示用户下载Excel电子表格,其中包含发生的每一行的所有错误.
这在我的开发机器上本地工作正常.然而,一旦部署到DEV环境,我会得到不同的结果.上传文件的第一次尝试导致流是什么,我在这里寻找的单词是什么,不可读?它正确地报告了它的长度,但试图将信息拉出来却一无所获.后续读取确实有数据,一切都按预期工作.
我会告诉你相关的代码pices.首先,简化了gen的HTML表单:
<form action="/Events/ImportLocal" enctype="multipart/form-data" method="post">
<input id="uploadFile" name="uploadFile" type="file" />
<input type="submit" value="Upload Events" />
</form>
Run Code Online (Sandbox Code Playgroud)
非常直截了当:
控制器动作(原谅这个烂摊子,我现在已经乱砍了几个小时了):
[HttpPost]
public ActionResult ImportLocal(HttpPostedFileBase uploadFile)
{
if (uploadFile == null || uploadFile.ContentLength <= 0)
{
BaseLogger.Info("uploadFile is null or content length is zero...");
//Error content returned to user
}
BaseLogger.InfoFormat("Community Import File Information: Content Length: {0}, Content Type: {1}, File Name: {2}",
uploadFile.ContentLength, uploadFile.ContentType, uploadFile.FileName);
//This logger …
Run Code Online (Sandbox Code Playgroud) 有没有办法让发布的文件(<input type="file" />
)参与ASP.NET MVC中的模型绑定,而无需在自定义模型绑定器中手动查看请求上下文,而无需创建仅将已发布文件作为输入的单独操作方法?
我原以为这会起作用:
class MyModel {
public HttpPostedFileBase MyFile { get; set; }
public int? OtherProperty { get; set; }
}
<form enctype="multipart/form-data">
<input type="file" name="MyFile" />
<input type="text" name="OtherProperty" />
</form>
public ActionResult Create(MyModel myModel) { ... }
Run Code Online (Sandbox Code Playgroud)
但鉴于上述情况,MyFile
甚至不是绑定上下文中值提供者值的一部分.(OtherProperty
当然是.)但如果我这样做,它会起作用:
public ActionResult Create(HttpPostedFileBase postedFile, ...) { ... }
Run Code Online (Sandbox Code Playgroud)
那么,当参数是模型时,为什么没有绑定发生,我怎样才能使它工作?我使用自定义模型绑定器没有问题,但是如何在不看的情况下在自定义模型绑定器中执行此操作Request.Files["MyFile"]
?
为了保持一致性,清晰度和可测试性,我希望我的代码能够自动绑定模型上的所有属性,包括绑定到已发布文件的属性,而无需手动检查请求上下文.我目前正在使用Scott Hanselman撰写的方法测试模型绑定.
或者我是以错误的方式解决这个问题?你怎么解决这个问题?或者,由于Request.Form和Request.Files之间的分离历史,这是不可能的设计?
asp.net-mvc defaultmodelbinder modelbinders httppostedfilebase
我有一个像这样的控制器:
public ActionResult Upload (int id, HttpPostedFileBase uploadFile)
{
....
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能确保uploadFile是一个图像(jpg,png等)
我试过了
using (var bitmapImage = new Bitmap (uploadFile.InputStream)) {..}
Run Code Online (Sandbox Code Playgroud)
如果无法创建bitmapImage,则会抛出ArgumentException.
有没有更好的方法,例如通过查看uploadFile.FileName?
我想测试以下代码行:
...
Bitmap uploadedPicture = Bitmap.FromStream(model.Picture.InputStream) as Bitmap;
...
Run Code Online (Sandbox Code Playgroud)
Picture是我的模型类型HttpPostedFileBase中的属性.所以我想模拟一个HttpPostedFileBase属性进行单元测试:
model.Picture = new Mock<HttpPostedFileBase>().Object;
Run Code Online (Sandbox Code Playgroud)
没问题.
现在我必须模拟InputStream,否则它是null:
model.Picture.InputStream = new Mock<Stream>().Object;
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为InputStream是只读的(没有setter方法):
public virtual Stream InputStream { get; }
Run Code Online (Sandbox Code Playgroud)
是否有一个良好而干净的方法来处理这个问题?一种解决方案是在我的单元测试的派生类中重写HttpPostedFileBase.还有其他想法吗?
在我的控制器中,因为我希望能够填写有关视频的一些细节并实际上传它,所以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);
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我有
@model LifeHighlightsShoeLace.Controllers.VideoController.VideoUploadModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
@using (Html.BeginForm("Create", "Video", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Video</legend>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.KalturaID)
</div> …
Run Code Online (Sandbox Code Playgroud) 我理解之间的关系HttpPostedFileBase
,并HttpPostedFileWrapper
为他们两个(即在单元测试/嘲讽)的,在需要方面.但是,为什么,当我在回报中设置断点时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 { get { return _myHttpPostedFile.ContentType; } }
}
Run Code Online (Sandbox Code Playgroud)
为了使其工作,我需要传递这样的参数:
GetFiles(new MyHttpPostedFileWrapper(new MyHttpPostedFile());
Run Code Online (Sandbox Code Playgroud)
这似乎是我质疑的诡计存在的地方..NET如何知道传递给它的字节是一个类型的类MyHttpPostedFile
,它应该采用该对象并将其作为参数传递给我的构造函数?
编辑#2:
我没有意识到ASP.NET …
模型
public partial class Assignment
{
public Assignment()
{
this.CourseAvailables = new HashSet<CourseAvailable>();
}
public string AssignmentID { get; set; }
public Nullable<System.DateTime> SubmissionDate { get; set; }
public string Status { get; set; }
public Nullable<decimal> Mark { get; set; }
public string Comments { get; set; }
public string FileLocation { get; set; }
public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}}
Run Code Online (Sandbox Code Playgroud)
调节器
public ActionResult Create(Assignment assignment)
{
if (ModelState.IsValid)
{
db.Assignments.Add(assignment);
db.SaveChanges();
return RedirectToAction("Index");
}
return …
Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc httppostedfilebase asp.net-mvc-3 asp.net-mvc-4
我正在尝试将图像上传到我的应用程序,但它总是返回null.我在这里找不到问题.你能帮我吗?这是我的代码.
模型
[Table("Slider")]
public partial class Slider : BaseModel
{
[Required]
[StringLength(200)]
public string FileName { get; set; }
[StringLength(200)]
public string Title { get; set; }
[StringLength(1000)]
public string Description { get; set; }
public int? Order { get; set; }
}
[NotMapped]
public class SliderImage : Slider
{
public HttpPostedFileBase ImageFile { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class …
Run Code Online (Sandbox Code Playgroud) asp.net-mvc file-upload httppostedfilebase twitter-bootstrap
asp.net-mvc ×7
c# ×4
file-upload ×3
.net ×1
image ×1
modelbinders ×1
moq ×1
razor ×1
unit-testing ×1
upload ×1