如何从文件上传器(input = file)中选择文件名到MVC中的模型

时间:2016-07-15 06:00:20

标签: jquery asp.net-mvc

我正在研究MVC中的批量插入,我想插入文本字段和文件(图像)。

我在HttpPostedFileBase []中获取文件。

但在模型中它(照片)显示为null。

型号: -

 public partial class User_Image
    {
        public int SrNo { get; set; }
        public string Name { get; set; }
        public string Photo { get; set; }
    }

查看: -

@if (Model != null && Model.Count > 0)
 {
     int j = 0;
     foreach (var i in Model)
    {
       <tr>
         <td>@Html.TextBoxFor(a => a[j].SrNo, new { @Value = Session["SrNo"], @class = "currnent_srno", @readonly = "true" })</td>
         <td>@Html.TextBoxFor(a => a[j].Name, new { @required = "required", @class = "myclass" })</td>
         <td>@Html.TextBoxFor(a => a[j].Photo, new { type = "file", @class = "fileuploader", @required = "required" })</td>
       </tr>
    }
}

控制器操作: -

    [HttpPost]
    public ActionResult BulkData(List<User_Image> ci, HttpPostedFileBase[] file, FormCollection fc)
    {
      if (ModelState.IsValid)
        {
            using (TestEntities dc = new TestEntities())
            {
                foreach (var i in ci)
                {
                    dc.User_Image.Add(i);
                }
                dc.SaveChanges();
                ViewBag.Message = "Data successfully saved!";
             }
         }
    }

jQuery: - (在所有输入文件中添加名称attr)

  $('input[type="file"]').attr('name', 'file');

2 个答案:

答案 0 :(得分:0)

模型应该是这样的

public partial class User_Image
{
    public int SrNo { get; set; }
    public string Name { get; set; }
    public string  Photo { get; set; }
}

View应该是这样的

foreach (var i in Model)
{
   <tr>          
      <td>@Html.HiddenFor(a => a.Photo, new { @class = "fileuploader", @required = "required" })</td>
      <td><input type="file" name="uploadFile" id="uploadFile" /></td>
  </tr>
}

Control应该是这样的

public ActionResult BulkData(List<User_Image> ci, string Photo, FormCollection fc)

请编写javascript函数,在上传时将convertata转换为base64字符串。

答案 1 :(得分:0)

我遇到了这个问题,并提出了解决此问题的替代方法。

而不是提交属性(在您的情况下为List)和图像作为参数来执行操作。将它们添加到formData对象中,并将该数据传递给ajax调用,如下所示,并将其保存并保存。

&#13;
&#13;
                var formData = new FormData();
                var totalFiles = document.getElementById("attachFiles").files.length;
                for (var i = 0; i < totalFiles; i++) {
                    var file = document.getElementById("attachFiles").files[i];

                    formData.append("attachFiles", file);
                }
                                               
                formData.append('SrNo', varsrno);
                formData.append('Name', varName);
                formData.append('Photo', varPhoto);

          $.ajax({
                type: "POST",
                url: "@Url.Action("BulkData", "Controller" )",
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false
                
        });
&#13;
&#13;
&#13;

&#13;
&#13;
        [HttpPost]
        public JsonResult BulkData()
        {

            string Title = Request.Form.Get("SrNo");
            string Oasid = Request.Form.Get("Name");
            string keyword = Request.Form.Get("Photo");

            HttpFileCollectionBase attachFiles = null;
            if (Request.Files.Count > 0)
                attachFiles = Request.Files[0];

            foreach(file in attachFiles)
            {
                file.SaveAs(path);
            }
        }
&#13;
&#13;
&#13;

希望它对你有所帮助。