上传文件与MVC 3 [英] Uploading files with MVC 3

查看:145
本文介绍了上传文件与MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我日渐消瘦learving MVC3。我有以下的code在我的控制器

  [HttpPost]
    公众的ActionResult认证(认证认可)
    {
        如果(ModelState.IsValid)
        {
            变种文件上传= Request.Files [0];
            VAR uploadPath =使用Server.Mappath(〜/ App_Data文件/上传);            使用(VAR FS =新的FileStream(Path.Combine(uploadPath,认可。pressCard.ToString()),FileMode.Create))
            {
                VAR缓冲=新的字节[fileUpload.InputStream.Length]
                fileUpload.InputStream.Read(缓冲液,0,buffer.Length);                fs.Write(缓冲液,0,buffer.Length);
            }            context.Accreditations.Add(认证);
            context.SaveChanges();            返回RedirectToAction(「指数」);
        }
           ViewBag.PossibleNationalities = context.Nationalities;
            ViewBag.PossibleNchis = context.Nchis;
            ViewBag.PossibleMedia = context.Media;
            ViewBag.PossibleEmploymentStatus = context.EmploymentStatus;
            返回视图(认证);    }
}

这里的景观:

  @using(Html.BeginForm(认可,家,新{ENCTYPE =的multipart / form-data的},FormMethod.Post))
{
   @ Html.ValidationSummary(真)
.............
.............< D​​IV CLASS =主编场>
       <输入类型=文件名称=pressCardID =pressCard数据-VAL-所需=preSS卡是必需的数据-VAL =真/>
         @ Html.ValidationMessageFor(型号=方式>模式pressCard)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Passport)
    < / DIV>
    < D​​IV CLASS =主编场>
       <输入类型=文件名称=护照ID =护照数据-VAL-所需=身份证/护照需要数据-VAL =真/>
        @ Html.ValidationMessageFor(型号=> model.Passport)
    < / DIV>

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

当我尝试上传,我得到了以下错误消息:

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

任何一个在那里与一个指向正确的方向?


抱歉延迟。这里是新的code

  [HttpPost]
公众的ActionResult认证(认证认可,HttpPostedFileBase护照)
    {
        如果(ModelState.IsValid)
        {
            VAR uploadPath =使用Server.Mappath(〜/ App_Data文件/上传);            使用(VAR FS =新的FileStream(Path.Combine(uploadPath,认可。pressCard.ToString()),FileMode.Create))
            {
                VAR缓冲=新的字节[Passport.InputStream.Length]
                Passport.InputStream.Read(缓冲液,0,buffer.Length);                fs.Write(缓冲液,0,buffer.Length);
            }            context.Accreditations.Add(认证);
            context.SaveChanges();            返回RedirectToAction(「指数」);
        }
           ViewBag.PossibleNationalities = context.Nationalities;
            ViewBag.PossibleNchis = context.Nchis;
            ViewBag.PossibleMedia = context.Media;
            ViewBag.PossibleEmploymentStatus = context.EmploymentStatus;
            返回视图(认证);    }
}


解决方案

难道真的上传数据?我建议你​​用这种方式。创建类型HttpPostedFileBase与同名作为输入字段并测试其内容length属性的参数。

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

检查该链接就会以最快的方式为你继续前进。

<一个href=\"http://stackoverflow.com/questions/4784225/mvc-3-file-upload-and-model-binding/4784292#4784292\">MVC 3文件上传和模型绑定

I'm growing thin learving mvc3. I have the following code in my controller

    [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);



    }
}

Here's the View:

@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>

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

When I try to upload, i get the following error message:

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Any one out there with a pointer to the right direction?


sorry for delay. Here's the new code

[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);



    }
}

解决方案

Are really uploading data? I'd suggest you use this way. Create a parameter of type HttpPostedFileBase with the same name as the input field and test for its content length property.

Don't forget to use the same name for the parameter and for the input tag.

Checking this link will the fastest way for you to move on.

MVC 3 file upload and model binding

这篇关于上传文件与MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆