我正在使用ASP.NET MVC4来开发Intranet应用程序。其中一个主要功能是允许用户上传将存储在我的数据库中的文件。为了做到这一点,我正在使用jQuery。但是,我不知道我必须做些什么来操纵上传的文件。我已经知道我必须将它们操纵到通讯控制器中,但在阅读了互联网上的一些提示之后,我只是看不出我的想法。
以下是我的观点:
@model BuSIMaterial.Models.ProductAllocationLog
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductAllocationLog</legend>
<div class="editor-label">
Date :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Date, new { @class = "datepicker"})
@Html.ValidationMessageFor(model => model.Date)
</div>
<div class="editor-label">
Message :
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>
<div class="editor-label">
Allocation :
</div>
<div class="editor-field">
@Html.DropDownList("Id_ProductAllocation", String.Empty)
@Html.ValidationMessageFor(model => model.Id_ProductAllocation)
</div>
<div class="demo" style="float:left; margin-top:5px;">
<div id="aupload" style = "border:2px dashed #ddd; width:100px; height:100px; margin-right:10px; padding:10px; float:left;"></div>
<div id="uploaded" style = "border: 1px solid #ddd; width:550px; height:102px; padding:10px; float:left; overflow-y:auto;"></div>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我绝对不会要求预先制作的代码示例,而只是为了继续进行。这将是非常友好的。
答案 0 :(得分:0)
您需要做三件事。首先,在您的视图中添加图片上传字段:
<input type="file" name="file-upload" />
..让你的表单'multipart'..
@using (Html.BeginForm("Action", "Controller", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}
...然后在您的控制器中,通过请求对象上的“文件”集合访问该文件:
Request.Files["file-upload"];
如果您希望使用ajax / jquery提交表单,那么还需要做更多的工作来序列化文件。这篇文章将帮助您:Sending multipart/formdata with jQuery.ajax
答案 1 :(得分:0)
@model MVCDemo.Models.tbl_Images
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductAllocationLog</legend>
<div class="editor-label">
Date :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Date, new { @class = "datepicker"})
@Html.ValidationMessageFor(model => model.Date)
</div>
<div class="editor-label">
Message :
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>
<div class="editor-label">
Allocation :
</div>
<div class="editor-field">
@Html.DropDownList("Id_ProductAllocation", String.Empty)
@Html.ValidationMessageFor(model => model.Id_ProductAllocation)
</div>
<div class="demo" style="float:left; margin-top:5px;">
<div id="aupload" style = "border:2px dashed #ddd; width:100px; height:100px; margin-right:10px; padding:10px; float:left;"></div>
<div id="uploaded" style = "border: 1px solid #ddd; width:550px; height:102px; padding:10px; float:left; overflow-y:auto;"></div>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>