在MVC5中使用扩展方法上传文件时出现问题

时间:2019-06-17 06:37:55

标签: c# asp.net-mvc-5

我创建了一个注册表,每个用户都可以在其中将他的信息以及上载文件发送到服务器。尽管我没有定义“必填”字段,但在上载文件供选择之前,将不会发送该表单。我认为控制器代码有问题,

我在控制器中编写的代码是:

         public ActionResult Create([Bind(Include =" ID,FullName,Mellicode,PostTime,Mobile,Telephone,Email,Age,Gender,Address,Postalcode,LastEducationalCertificate,FieldOfStudy,JobExperience,MaritalStatus,Comment,FilePathName,Invitation,Invalidity,Recruitment")] RegisterForm registerForm, HttpPostedFileBase UploadFile)

            {

        var myUniqueFileName = string.Format(@"{0}.txt",Guid.NewGuid());


        if (ModelState.IsValid)
        {
             string strFileExtension = System.IO.Path.GetExtension(UploadFile.FileName).ToUpper();


            string strContentType = UploadFile.ContentType.ToUpper();

                    if (UploadFile.ContentLength > 100 * 1024)
                {
                           return View("Error");
            }

            else if(UploadFile.ContentLength<100*1024 && UploadFile !=null) 
            {
                registerForm.FilePathName = myUniqueFileName + UploadFile.FileName;

                if (System.IO.Directory.Exists(strPath) == false)
                {
                    System.IO.Directory.CreateDirectory(strPath);
                }

                string strPathName =
                    string.Format("{0}\\{1}", strPath, registerForm.FilePathName);

                UploadFile.SaveAs(strPathName);
            }

            db.Registers.Add(registerForm);
            db.SaveChanges();
            return RedirectToAction("Back");
        }
        return View(registerForm);
        }

要上传,还使用以下扩展方法:

         public static class UploadHelper
       {
public static MvcHtmlString Upload(this HtmlHelper helper, string name, object htmlAttributes = null)
{
    //helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression))
    TagBuilder input = new TagBuilder("input");
    input.Attributes.Add("type", "file");
    input.Attributes.Add("id", helper.ViewData.TemplateInfo.GetFullHtmlFieldId(name));
    input.Attributes.Add("name", helper.ViewData.TemplateInfo.GetFullHtmlFieldName(name));
    input.Attributes.Add("class", "required");


    if (htmlAttributes != null)
    {
        var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
        input.MergeAttributes(attributes);
    }

    return new MvcHtmlString(input.ToString());
}

}

代码视图上传:

          <div class="form-group">
          @Html.LabelFor(model => model.FilePathName, htmlAttributes: new { @class = "control-label col-md-10" })
                  <div class="col-md-10">
                      @Html.Upload("UploadFile", new { htmlAttributes = new { @class = "form-control demoInputBox ", type = "file", runat = "server" } })
    @Html.ValidationMessageFor(model => model.FilePathName, "", new { @class = "text-danger" })
                  </div>
              </div>

          </div>

如果有人可以感谢我,我想使用这种方法

0 个答案:

没有答案