<legend id='UpKNh'><style id='UpKNh'><dir id='UpKNh'><q id='UpKNh'></q></dir></style></legend>

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

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

        <i id='UpKNh'><tr id='UpKNh'><dt id='UpKNh'><q id='UpKNh'><span id='UpKNh'><b id='UpKNh'><form id='UpKNh'><ins id='UpKNh'></ins><ul id='UpKNh'></ul><sub id='UpKNh'></sub></form><legend id='UpKNh'></legend><bdo id='UpKNh'><pre id='UpKNh'><center id='UpKNh'></center></pre></bdo></b><th id='UpKNh'></th></span></q></dt></tr></i><div id='UpKNh'><tfoot id='UpKNh'></tfoot><dl id='UpKNh'><fieldset id='UpKNh'></fieldset></dl></div>
        • <bdo id='UpKNh'></bdo><ul id='UpKNh'></ul>
      1. ASP MVC 文件上传 HttpPostedFileBase 为空

        时间:2023-06-03

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

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

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

                  问题描述

                  限时送ChatGPT账号..

                  在我的控制器中,因为我希望能够填写有关视频的一些详细信息并实际上传它,所以 Video 类不需要实际的视频,因为它将被传递给另一个 Web 服务.

                  In my controller I have, because I wanted to be able to fill out some details about the video and actually upload it, the Video class doesn't need the actual video because it's going to be passed to another web service.

                  public class VideoUploadModel
                      {
                          public HttpPostedFileBase vid { get; set; }
                          public Video videoModel { get; set; }
                      }
                  
                      //
                      // POST: /Video/Create
                      [HttpPost]
                      public ActionResult Create(VideoUploadModel VM)
                      {
                          if (ModelState.IsValid)
                          {
                              db.Videos.AddObject(VM.videoModel);
                              db.SaveChanges();
                              return RedirectToAction("Index");  
                          }
                  
                          ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
                          return View(VM);
                      }
                  

                  在我看来我有

                  @model LifeHighlightsShoeLace.Controllers.VideoController.VideoUploadModel
                  @{
                     ViewBag.Title = "Create";
                  }
                  
                  <h2>Create</h2>
                  
                  <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
                  

                  @using (Html.BeginForm("Create", "Video", FormMethod.Post, new { enctype = "multipart/form-data" }))
                  {
                  @Html.ValidationSummary(true)
                  <fieldset>
                      <legend>Video</legend>
                  
                      <div class="editor-label">
                          @Html.LabelFor(model => model.videoModel.KalturaID)
                      </div>
                      <div class="editor-field">
                          @Html.EditorFor(model => model.videoModel.KalturaID)
                          @Html.ValidationMessageFor(model => model.videoModel.KalturaID)
                      </div>
                  
                      <div class="editor-label">
                          @Html.LabelFor(model => model.videoModel.Size)
                      </div>
                      <div class="editor-field">
                          @Html.EditorFor(model => model.videoModel.Size)
                          @Html.ValidationMessageFor(model => model.videoModel.Size)
                      </div>
                  
                      <div class="editor-label">
                          @Html.LabelFor(model => model.videoModel.Date)
                      </div>
                      <div class="editor-field">
                          @Html.EditorFor(model => model.videoModel.Date)
                          @Html.ValidationMessageFor(model => model.videoModel.Date)
                      </div>
                  
                      <div class="editor-label">
                          @Html.LabelFor(model => model.videoModel.UploadedBy)
                      </div>
                      <div class="editor-field">
                          @Html.EditorFor(model => model.videoModel.UploadedBy)
                          @Html.ValidationMessageFor(model => model.videoModel.UploadedBy)
                      </div>
                  
                      <div class="editor-label">
                          @Html.LabelFor(model => model.videoModel.UserId, "User")
                      </div>
                  
                      <div class="editor-field">
                          @Html.DropDownList("UserId", String.Empty)
                          @Html.ValidationMessageFor(model => model.videoModel.UserId)
                      </div>
                      <div class="editor-field">
                      <input name="model.vid" type="file" />
                      </div>
                      <p>
                          <input type="submit" value="Create" />
                      </p>
                  </fieldset>
                  

                  }

                  当我提交表单时,VM 的 videoModel 部分已填写,但实际文件为空.有什么想法吗?

                  When I submit the form the videoModel part of VM is filled out but vid the actual file is null. Any ideas?

                  推荐答案

                  根据OP评论更新

                  在 web.config 文件中设置最大文件长度改变?"到您希望最大的文件大小,例如 65536 是 64MB

                  set the Max file length in the web.config file Change the "?" to a file size that you want to be your max, for example 65536 is 64MB

                  <configuration>
                    <system.web>
                      <httpRuntime maxRequestLength="?" /> 
                    </system.web>
                  </configuration>
                  

                  您不能将文件添加到模型中,它将在它自己的字段中,而不是模型的一部分

                  You can't add the file to the model, it will be in it's own field not part of the model

                  <input name="videoUpload" type="file" />
                  

                  您的操作不正确.它需要接受文件作为它自己的参数(或者如果多个使用 IEnumerable 作为参数类型)

                  Your action is incorrect. It needs to accept the file as it's own parameter (or if multiple use IEnumerable<HttpPostedFileBase> as the parameter type)

                  [HttpPost]
                  public ActionResult Create(VideoUploadModel VM, HttpPostedFileBase videoUpload)
                  {
                      if (ModelState.IsValid)
                      {
                          if(videoUpload != null) { // save the file
                              var serverPath = server.MapPath("~/files/" + newName);
                              videoUpload.SaveAs(serverPath);
                          }
                  
                          db.SaveChanges();
                          return RedirectToAction("Index");  
                      }
                  
                      ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
                      return View(VM);
                  }
                  

                  如果您允许选择多个文件,则必须允许这样做

                  If you were allowing multiple files to be selected you have to allow for that

                  [HttpPost]
                  public ActionResult Create(VideoUploadModel VM, IEnumerable<HttpPostedFileBase> videoUpload)
                  {
                      if (ModelState.IsValid)
                      {
                          if(videoUpload != null) { // save the file
                              foreach(var file in videoUpload) {
                                  var serverPath = server.MapPath("~/files/" + file.Name);
                                  file.SaveAs(serverPath);
                              }
                          }
                  
                          db.SaveChanges();
                          return RedirectToAction("Index");  
                      }
                  
                      ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
                      return View(VM);
                  }
                  

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

                  上一篇:C# 中的后台智能传输服务 下一篇:C# - 我无法引用 HttpPostedFileBase

                  相关文章

                  最新文章

                • <tfoot id='IFUO8'></tfoot>
                    1. <small id='IFUO8'></small><noframes id='IFUO8'>

                      <legend id='IFUO8'><style id='IFUO8'><dir id='IFUO8'><q id='IFUO8'></q></dir></style></legend>

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