<small id='qgtuW'></small><noframes id='qgtuW'>

<legend id='qgtuW'><style id='qgtuW'><dir id='qgtuW'><q id='qgtuW'></q></dir></style></legend>
<i id='qgtuW'><tr id='qgtuW'><dt id='qgtuW'><q id='qgtuW'><span id='qgtuW'><b id='qgtuW'><form id='qgtuW'><ins id='qgtuW'></ins><ul id='qgtuW'></ul><sub id='qgtuW'></sub></form><legend id='qgtuW'></legend><bdo id='qgtuW'><pre id='qgtuW'><center id='qgtuW'></center></pre></bdo></b><th id='qgtuW'></th></span></q></dt></tr></i><div id='qgtuW'><tfoot id='qgtuW'></tfoot><dl id='qgtuW'><fieldset id='qgtuW'></fieldset></dl></div>

    <bdo id='qgtuW'></bdo><ul id='qgtuW'></ul>

      1. <tfoot id='qgtuW'></tfoot>

        使用 MVC 3 上传文件

        时间:2023-06-03
        <tfoot id='YDYro'></tfoot>

          <i id='YDYro'><tr id='YDYro'><dt id='YDYro'><q id='YDYro'><span id='YDYro'><b id='YDYro'><form id='YDYro'><ins id='YDYro'></ins><ul id='YDYro'></ul><sub id='YDYro'></sub></form><legend id='YDYro'></legend><bdo id='YDYro'><pre id='YDYro'><center id='YDYro'></center></pre></bdo></b><th id='YDYro'></th></span></q></dt></tr></i><div id='YDYro'><tfoot id='YDYro'></tfoot><dl id='YDYro'><fieldset id='YDYro'></fieldset></dl></div>
            <bdo id='YDYro'></bdo><ul id='YDYro'></ul>

                1. <legend id='YDYro'><style id='YDYro'><dir id='YDYro'><q id='YDYro'></q></dir></style></legend>

                  <small id='YDYro'></small><noframes id='YDYro'>

                    <tbody id='YDYro'></tbody>
                  本文介绍了使用 MVC 3 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  离开 mvc3,我越来越瘦了.我的控制器中有以下代码

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

                  这是视图:

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

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

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

                  推荐答案

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

                  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.

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

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

                  MVC 3 文件上传和模型绑定

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

                  上一篇:在 C# 中暂停/恢复上传 下一篇:以编程方式设置要在网页中上传的文件名

                  相关文章

                  最新文章

                  1. <i id='cY7bE'><tr id='cY7bE'><dt id='cY7bE'><q id='cY7bE'><span id='cY7bE'><b id='cY7bE'><form id='cY7bE'><ins id='cY7bE'></ins><ul id='cY7bE'></ul><sub id='cY7bE'></sub></form><legend id='cY7bE'></legend><bdo id='cY7bE'><pre id='cY7bE'><center id='cY7bE'></center></pre></bdo></b><th id='cY7bE'></th></span></q></dt></tr></i><div id='cY7bE'><tfoot id='cY7bE'></tfoot><dl id='cY7bE'><fieldset id='cY7bE'></fieldset></dl></div>
                  2. <small id='cY7bE'></small><noframes id='cY7bE'>

                      <bdo id='cY7bE'></bdo><ul id='cY7bE'></ul>
                    <legend id='cY7bE'><style id='cY7bE'><dir id='cY7bE'><q id='cY7bE'></q></dir></style></legend>
                  3. <tfoot id='cY7bE'></tfoot>