File upload 将数据写入数据库,包括图像
我有一个MVC4项目,我需要将数据写入数据库,包括“创建”视图中的图像 我找到了一些关于如何上传图像的例子。但是没有一个示例具有一些额外的输入字段以及html文件上载控件 我想知道是否可以浏览到图像并填写其他字段,然后按“发送”按钮,然后接收控制器中的所有数据,尤其是此方法:File upload 将数据写入数据库,包括图像,file-upload,asp.net-mvc-4,form-data,File Upload,Asp.net Mvc 4,Form Data,我有一个MVC4项目,我需要将数据写入数据库,包括“创建”视图中的图像 我找到了一些关于如何上传图像的例子。但是没有一个示例具有一些额外的输入字段以及html文件上载控件 我想知道是否可以浏览到图像并填写其他字段,然后按“发送”按钮,然后接收控制器中的所有数据,尤其是此方法: [HttpPost] public ActionResult Create(FormCollection formCollection) { foreach (var key in formCollectio
[HttpPost]
public ActionResult Create(FormCollection formCollection)
{
foreach (var key in formCollection.AllKeys)
{
if (key == "file")
{
foreach (string s in Request.Files)
{
HttpPostedFileBase file = Request.Files[s];
if (file.ContentLength > 0)
{
}
}
}
}
}
这是一种观点:
@using (Html.BeginForm("Create", "Item", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Item</legend>
<div class="editor-label">
Description:
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
Image:
</div>
<input type="file" name="file" id="file" />
<p>
<input type="submit" value="send" />
</p>
</fieldset>
}
@使用(Html.BeginForm(“Create”、“Item”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@Html.ValidationSummary(true)
项目
说明:
@EditorFor(model=>model.Description)
@Html.ValidationMessageFor(model=>model.Description)
图片:
}
在这种情况下,Request.Files
始终为空
我遗漏了什么?您还应该能够使用强类型编辑模型来完成此操作:
public class MyDataEditModel
{
public string Description { get; set; }
public HttpPostedFileBase File { get; set; }
}
[HttpPost]
public ActionResult Create(MyDataEditModel model)
{
// is model.File null here?
}
您还应该能够使用强类型编辑模型执行此操作:
public class MyDataEditModel
{
public string Description { get; set; }
public HttpPostedFileBase File { get; set; }
}
[HttpPost]
public ActionResult Create(MyDataEditModel model)
{
// is model.File null here?
}
您可以按照下面的说明在ASP.NET MVC中上载图像
这里的动作如下所示
[HttpPost]
public ActionResult Index(UserModel model, HttpPostedFileBase file)
{}
在发布文件的同时,还发布了一个UserModel,它可以从页面中获取您想要的其他数据。您可以按照下面的说明在ASP.NET MVC中上载图像
这里的动作如下所示
[HttpPost]
public ActionResult Index(UserModel model, HttpPostedFileBase file)
{}
在发布文件的同时,还发布了一个UserModel,它从页面中获取其他数据,这是您所需要的。从您的代码中,一切看起来都很好。您是否已检查firebug以再次检查正在发送的内容类型
多部分/表单数据
。您是否已检查firebug以再次检查正在发送的内容类型<代码>多部分/表单数据代码>?