我的应用程序允许用户通过选择带有@Html.TextBoxFor(m => m.Image, new {type = "file"})
的图像来上传单个图像。如果有任何验证错误,我将丢失其选定的文件。因此,我需要通过If(!ModelState.IsValid)
代码块临时保存它:
public ActionResult Create(MyModel model) {
if (!ModelState.IsValid)
{
if(model.Image != null && model.Image.ContentLength > 0) {
var displayName = Path.GetFileName(model.Image.FileName);
var fileExtension = Path.GetExtension(displayName);
var fileName = string.Format("{0}{1}", Guid.NewGuid(), fileExtension);
var path = Path.Combine(Server.MapPath("~/App_Data/Files"), fileName);
model.Image.SaveAs(path);
model.displayName = displayName;
model.FilePath = path;
return View(model);
}
}
我在视图中使用@Html.HiddenFor(m => m.FilePath)
。
model.Image
类型为HttpPostedFileBase
。我必须以某种方式重新获得用户在HttpPostedFileBase
之后选择的图像为!ModelState.IsValid
,以便能够将其保存在数据库中。是否有机会在@Html.TextBoxFor(m => m.Image, new {type = "file"})
内传回信息?
How I later convert HttpPostedFileBase to byte[], in order to store image-data in database.
我不知道如何使用我拥有的工具。
修改:模型属性:
public string displayName { get; set; }
public string FilePath { get; set; }
public HttpPostedFileBase Image { get; set; }
答案 0 :(得分:1)
出于安全原因,您无法设置文件输入的值(只能由用户在浏览器中选择文件来设置)。
在视图中,您需要进行条件检查才能显示文件名和路径(最初将显示null
)
@if (Model.displayName != null)
{
<div>@Model.displayName</div> // let the user know that its been uploaded
@Html.HiddenFor(m => m.FilePath)
}
然后在控制器中,如果ModelState
有效,则需要有条件地检查FilePath
的值。如果您提交并且ModelState
无效,则FilePath
的值现在将包含用户重新提交时保存文件的路径。但是,如果ModelState
在初始提交时有效,则FilePath
的值将为null
您需要控制器代码(请注意,这假设MyModel
实际上是一个视图模型,并且您有一个关联的数据模型,其中包含用于将文件保存在数据库表中的属性public byte[] Image { get; set; }
public ActionResult Create(MyModel model)
{
if (!ModelState.IsValid)
{
.... // code as above to save temporary file and return the view
}
// Initialize an instance of your data model
var dataModel = new .... // assumes this contains a property byte[] Image
if (model.FilePath == null)
{
// The code above has never run so read from the HttpPostedFileBase property
if(model.Image != null && model.Image.ContentLength > 0) {
{
MemoryStream target = new MemoryStream();
model.Image.InputStream.CopyTo(target);
dataModel.Image = target.ToArray();
}
}
else
{
// Read from the temporary file
dataModel.Image = System.IO.File.ReadAllBytes(filename);
.... // Delete the temporary file
}
// Map other properties of your view model to the data model
// Save and redirect
}