2013-03-20 59 views
0

我正在使用ASP.NET MVC4来开发一个Intranet应用程序。其中一个主要功能是允许用户上传将存储在我的数据库中的文件。为了做到这一点,我使用jQuery。但是,我不知道我必须做什么才能操纵上传的文件。我已经知道,我必须操纵他们进入通讯员控制器,但在阅读了互联网上的一些提示后,我看不出我该怎么做。MVC4检索上传的文件

这是我的观点:

@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

你需要做三件事。首先,图像上传字段添加到您的视图:

<input type="file" name="file-upload" /> 

..make表单“多” ..

@using (Html.BeginForm("Action", "Controller", null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
} 

...然后在你的控制器,通过“文件访问该文件请求对象“集合:

Request.Files["file-upload"]; 

如果您希望与阿贾克斯/ jQuery的提交表单,然后有一点点更多的工作要做序列化的文件。这篇文章将帮助你:Sending multipart/formdata with jQuery.ajax

+0

谢谢,我会试试看。 – Traffy 2013-03-20 11:58:17

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>