Asp.net mvc MVC 3文件上传和模型绑定
我有一个可以工作的表单上传,但是我想为我的数据库传递模型信息,以便用不同的名称保存文件 以下是我的观点:Asp.net mvc MVC 3文件上传和模型绑定,asp.net-mvc,asp.net-mvc-3,razor,Asp.net Mvc,Asp.net Mvc 3,Razor,我有一个可以工作的表单上传,但是我想为我的数据库传递模型信息,以便用不同的名称保存文件 以下是我的观点: @model CertispecWeb.Models.Container @{ ViewBag.Title = "AddDocuments"; } <h2>AddDocuments</h2> @Model.ContainerNo @using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Po
@model CertispecWeb.Models.Container
@{
ViewBag.Title = "AddDocuments";
}
<h2>AddDocuments</h2>
@Model.ContainerNo
@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<input type='file' name='file' id='file' />
<input type="submit" value="submit" />
}
模型信息不会传递给控制器。我已经读到,我可能需要更新模型,我该怎么做?您的表单不包含文件以外的任何输入标记,因此在控制器操作中,您不能期望得到上传文件以外的任何内容(这就是发送到服务器的全部内容)。实现这一点的一种方法是包含一个包含模型id的隐藏标记,该标记将允许您从发布到的控制器操作中的数据存储中检索模型(如果用户不需要修改模型,只需附加一个文件,请使用该标记): 另一方面,如果希望允许用户修改此模型,则需要为要发送到服务器的模型的每个字段包含适当的输入字段:
@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(x => x.Prop1)
@Html.TextBoxFor(x => x.Prop2)
@Html.TextBoxFor(x => x.Prop3)
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
}
如果您不会总是将图像发布到您的操作中,您可以执行以下操作:
[HttpPost]
public ActionResult Uploadfile(Container container, HttpPostedFileBase file)
{
//do container stuff
if (Request.Files != null)
{
foreach (string requestFile in Request.Files)
{
HttpPostedFileBase file = Request.Files[requestFile];
if (file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
string directory = Server.MapPath("~/App_Data/uploads/");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string path = Path.Combine(directory, fileName);
file.SaveAs(path);
}
}
}
}
解决
模型
控制器
public class BookController : Controller
{
[HttpPost]
public ActionResult Create(Book model, IEnumerable<HttpPostedFileBase> fileUpload)
{
throw new NotImplementedException();
}
}
公共类BookController:控制器
{
[HttpPost]
公共操作结果创建(书本模型、IEnumerable文件上传)
{
抛出新的NotImplementedException();
}
}
和视图
@using (Html.BeginForm("Create", "Book", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.EditorFor(m => m)
<input type="file" name="fileUpload[0]" /><br />
<input type="file" name="fileUpload[1]" /><br />
<input type="file" name="fileUpload[2]" /><br />
<input type="submit" name="Submit" id="SubmitMultiply" value="Upload" />
}
@使用(Html.BeginForm(“Create”、“Book”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@EditorFor(m=>m)
}
注意:控制器操作参数的标题必须与输入元素的名称匹配
IEnumerable fileUpload
->name=“fileUpload[0]”
文件上传
必须匹配多个文件;请注意输入的较新“多个”属性:
表格:
@使用(Html.BeginForm(“FileImport”、“Import”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
文件名:
}
控制器:
[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"),
containers.ContainerNo);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult FileImport(IEnumerable<HttpPostedFileBase> files)
{
return View();
}
[HttpPost]
公共操作结果文件导入(IEnumerable文件)
{
返回视图();
}
首先从下面的url下载jquery.form.js文件
用cshtml编写下面的代码
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmTemplateUpload" }))
{
<div id="uploadTemplate">
<input type="text" value="Asif" id="txtname" name="txtName" />
<div id="dvAddTemplate">
Add Template
<br />
<input type="file" name="file" id="file" tabindex="2" />
<br />
<input type="submit" value="Submit" />
<input type="button" id="btnAttachFileCancel" tabindex="3" value="Cancel" />
</div>
<div id="TemplateTree" style="overflow-x: auto;"></div>
</div>
<div id="progressBarDiv" style="display: none;">
<img id="loading-image" src="~/Images/progress-loader.gif" />
</div>
}
<script type="text/javascript">
$(document).ready(function () {
debugger;
alert('sample');
var status = $('#status');
$('#frmTemplateUpload').ajaxForm({
beforeSend: function () {
if ($("#file").val() != "") {
//$("#uploadTemplate").hide();
$("#btnAction").hide();
$("#progressBarDiv").show();
//progress_run_id = setInterval(progress, 300);
}
status.empty();
},
success: function () {
showTemplateManager();
},
complete: function (xhr) {
if ($("#file").val() != "") {
var millisecondsToWait = 500;
setTimeout(function () {
//clearInterval(progress_run_id);
$("#uploadTemplate").show();
$("#btnAction").show();
$("#progressBarDiv").hide();
}, millisecondsToWait);
}
status.html(xhr.responseText);
}
});
});
</script>
我得到的
file
是null
和Request.Files.Count
也是0,如果表单是AjaxForm
并且也有routeValue
,会有什么区别吗?请参阅相关问题此解决方案是我为多个文件找到的唯一解决方案。谢谢分享你的代码。
public class BookController : Controller
{
[HttpPost]
public ActionResult Create(Book model, IEnumerable<HttpPostedFileBase> fileUpload)
{
throw new NotImplementedException();
}
}
@using (Html.BeginForm("Create", "Book", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.EditorFor(m => m)
<input type="file" name="fileUpload[0]" /><br />
<input type="file" name="fileUpload[1]" /><br />
<input type="file" name="fileUpload[2]" /><br />
<input type="submit" name="Submit" id="SubmitMultiply" value="Upload" />
}
@using (Html.BeginForm("FileImport","Import",FormMethod.Post, new {enctype = "multipart/form-data"}))
{
<label for="files">Filename:</label>
<input type="file" name="files" multiple="true" id="files" />
<input type="submit" />
}
[HttpPost]
public ActionResult FileImport(IEnumerable<HttpPostedFileBase> files)
{
return View();
}
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmTemplateUpload" }))
{
<div id="uploadTemplate">
<input type="text" value="Asif" id="txtname" name="txtName" />
<div id="dvAddTemplate">
Add Template
<br />
<input type="file" name="file" id="file" tabindex="2" />
<br />
<input type="submit" value="Submit" />
<input type="button" id="btnAttachFileCancel" tabindex="3" value="Cancel" />
</div>
<div id="TemplateTree" style="overflow-x: auto;"></div>
</div>
<div id="progressBarDiv" style="display: none;">
<img id="loading-image" src="~/Images/progress-loader.gif" />
</div>
}
<script type="text/javascript">
$(document).ready(function () {
debugger;
alert('sample');
var status = $('#status');
$('#frmTemplateUpload').ajaxForm({
beforeSend: function () {
if ($("#file").val() != "") {
//$("#uploadTemplate").hide();
$("#btnAction").hide();
$("#progressBarDiv").show();
//progress_run_id = setInterval(progress, 300);
}
status.empty();
},
success: function () {
showTemplateManager();
},
complete: function (xhr) {
if ($("#file").val() != "") {
var millisecondsToWait = 500;
setTimeout(function () {
//clearInterval(progress_run_id);
$("#uploadTemplate").show();
$("#btnAction").show();
$("#progressBarDiv").hide();
}, millisecondsToWait);
}
status.html(xhr.responseText);
}
});
});
</script>
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public void Upload(HttpPostedFileBase file, string txtname )
{
try
{
string attachmentFilePath = file.FileName;
string fileName = attachmentFilePath.Substring(attachmentFilePath.LastIndexOf("\\") + 1);
}
catch (Exception ex)
{
}
}