使用MVC 3上传文件

时间:2011-09-19 12:48:46

标签: c# file asp.net-mvc-3 upload

我越来越瘦了mvc3。我的控制器中有以下代码

    [HttpPost]
    public ActionResult Accreditation(Accreditation accreditation)
    {
        if (ModelState.IsValid)
        {
            var fileUpload = Request.Files[0];
            var uploadPath = Server.MapPath("~/App_Data/uploads");

            using (var fs = new FileStream(Path.Combine(uploadPath, accreditation.PressCard.ToString()), FileMode.Create))
            {
                var buffer = new byte[fileUpload.InputStream.Length];
                fileUpload.InputStream.Read(buffer, 0, buffer.Length);

                fs.Write(buffer, 0, buffer.Length);
            }

            context.Accreditations.Add(accreditation);
            context.SaveChanges();

            return RedirectToAction("Index");  
        }


           ViewBag.PossibleNationalities = context.Nationalities;
            ViewBag.PossibleNchis = context.Nchis;
            ViewBag.PossibleMedia = context.Media;
            ViewBag.PossibleEmploymentStatus = context.EmploymentStatus;
            return View(accreditation);



    }
}

这是视图:

@using (Html.BeginForm("Accreditation", "Home", new { enctype = "multipart/form-data" },    FormMethod.Post))
{
   @Html.ValidationSummary(true)
.............
.............

<div class="editor-field">
       <input type="file" name="PressCard" id="PressCard" data-val-required="Press card is required" data-val="true"/>
         @Html.ValidationMessageFor(model => model.PressCard)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Passport)
    </div>
    <div class="editor-field">
       <input type="file" name="Passport" id="Passport" data-val-required="ID/Passport is required" data-val="true"/>
        @Html.ValidationMessageFor(model => model.Passport)
    </div>

....... ........     

当我尝试上传时,收到以下错误消息:

异常详细信息:System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。 参数名称:index

那里有一个指向正确方向的人吗?


抱歉抱歉。这是新代码

[HttpPost]
public ActionResult Accreditation(Accreditation accreditation, HttpPostedFileBase Passport)
    {
        if (ModelState.IsValid)
        {
            var uploadPath = Server.MapPath("~/App_Data/uploads");

            using (var fs = new FileStream(Path.Combine(uploadPath, accreditation.PressCard.ToString()), FileMode.Create))
            {
                var buffer = new byte[Passport.InputStream.Length];
                Passport.InputStream.Read(buffer, 0, buffer.Length);

                fs.Write(buffer, 0, buffer.Length);
            }

            context.Accreditations.Add(accreditation);
            context.SaveChanges();

            return RedirectToAction("Index");  
        }


           ViewBag.PossibleNationalities = context.Nationalities;
            ViewBag.PossibleNchis = context.Nchis;
            ViewBag.PossibleMedia = context.Media;
            ViewBag.PossibleEmploymentStatus = context.EmploymentStatus;
            return View(accreditation);



    }
}

3 个答案:

答案 0 :(得分:4)

真的上传数据吗?我建议你用这种方式。创建一个HttpPostedFileBase类型的参数,其中同名作为输入字段,并测试其内容长度属性。

不要忘记为参数和输入标记使用相同的名称。

检查此链接是您继续前进的最快方式。

MVC 3 file upload and model binding

答案 1 :(得分:1)

直接获取您发布的文件:

以下是关于SO的讨论:MVC 3 file upload and model binding

[HttpPost]
public ActionResult Accreditation(Accreditation accreditation, HttpPostedFileBase Passport)
{
    ...
}

答案 2 :(得分:1)

var fileUpload = Request.Files[0];是你有异常的行,不是吗?您应该希望该文件存储在类Accreditation的属性中,而不是Request.Files中。

因此,PressCard中的属性PassportAccreditation都需要HttpPostedFileBase类型的属性,然后在代码中使用这些属性。

相关问题