File Upload ASP.NET MVC 3.0

后端 未结 21 997
无人共我
无人共我 2020-11-22 01:09

(Preface: this question is about ASP.NET MVC 3.0 which was released in 2011, it is not about ASP.NET Core 3.0 which was released in 2019)

I want to

21条回答
  •  终归单人心
    2020-11-22 01:31

    Html:

    @using (Html.BeginForm("StoreMyCompany", "MyCompany", FormMethod.Post, new { id = "formMyCompany", enctype = "multipart/form-data" }))
    {
       
    @Html.LabelFor(model => model.modelMyCompany.Logo, htmlAttributes: new { @class = "control-label col-md-3" })

    }

    Code Behind:

    public ActionResult StoreMyCompany([Bind(Exclude = "Logo")]MyCompanyVM model)
    {
        try
        {        
            byte[] imageData = null;
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase objFiles = Request.Files["Logo"];
    
                using (var binaryReader = new BinaryReader(objFiles.InputStream))
                {
                    imageData = binaryReader.ReadBytes(objFiles.ContentLength);
                }
            }
    
            if (imageData != null && imageData.Length > 0)
            {
               //Your code
            }
    
            dbo.SaveChanges();
    
            return RedirectToAction("MyCompany", "Home");
    
        }
        catch (Exception ex)
        {
            Utility.LogError(ex);
        }
    
        return View();
    }
    

提交回复
热议问题