mvc upload file with model - second parameter posted file is null

后端 未结 4 1275
南旧
南旧 2020-12-05 13:35

I have a simple model with 1 string property which I render on a simple view.

the view looks like the following:

@using (Html.BeginForm(\"UploadFile\         


        
相关标签:
4条回答
  • 2020-12-05 14:05

    Why not add the uploaded files to your model like this:

    public class UploadFileModel 
    {
        public UploadFileModel()
        {
            Files = new List<HttpPostedFileBase>();
        }
    
        public List<HttpPostedFileBase> Files { get; set; }
        public string FirstName { get; set; }
        // Rest of model details
    }
    

    Then change your view to this:

    @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { encType="multipart/form-data" }))
    {
        @Html.TextBoxFor(m => m.FirstName)
        <br /><br />
    
        @Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })<br /><br />
        <input type="submit" value="submit me" name="submitme" id="submitme" />
    }
    

    Then your files will be posted back as follows:

    public ActionResult UploadFile(UploadFileModel model)
    {
        var file = model.Files[0];
        return View(model);
    }
    
    0 讨论(0)
  • 2020-12-05 14:06

    Alternatively, (if acceptable) remove the [Required] validation annotation for your file from your model and check for the file in your Controller action, and add an error if not found:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ActionWithFileUpload(ViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            if (Request.Files.Count > 0)
            {
                var postedFile = Request.Files[0];
                if (postedFile != null && postedFile.ContentLength > 0)
                {
                    string imagesPath = HttpContext.Server.MapPath("~/Content/Images"); // Or file save folder, etc.
                    string extension = Path.GetExtension(postedFile.FileName);
                    string newFileName = $"NewFile{extension}";
                    string saveToPath = Path.Combine(imagesPath, newFileName);
                    postedFile.SaveAs(saveToPath);
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "File not selected.");
            }
        }
    
        return RedirectToAction("Index"); // Or return view, etc.
    }
    
    0 讨论(0)
  • 2020-12-05 14:19

    Change your name file to fileUpload and enctype it's work

    @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype="multipart/form-data" }))
    {
        @Html.TextBoxFor(m => m.FirstName)
        <br /><br />
    
        <input type="file" name="fileUpload" /><br /><br />
        <input type="submit" value="submit me" name="submitme" id="submitme" />
    }
    
    [HttpPost]
    public ActionResult UploadFile(UploadFileModel model, HttpPostedFileBase fileUpload)
    {
       // DO Stuff
       return View(model);
    }
    
    0 讨论(0)
  • 2020-12-05 14:23

    For dealing with a single file input you can define a HttpPostedFileBase property within the ViewModel:

    public class SomeModel() 
    { 
        public SomeModel() 
        {
        }
    
        public HttpPostedFileBase SomeFile { get; set; }
    }
    

    And then implement it in the following way:

    View:

    @model SomeModel

    @using (Html.BeginForm(
        "Submit", 
        "Home", 
        FormMethod.Post, 
        new { enctype="multipart/form-data" }))
    {
        @Html.TextBoxFor(m => m.SomeFile, new { type = "file" })
        <input type="submit" value="Upload" 
            name="UploadButton" id="UploadButton" />
    }
    

    Controller:

    [HttpPost]
    public ActionResult Submit(SomeModel model)
    {
        // do something with model.SomeFile
    
        return View();
    }
    

    In case you need to deal with multiple files, you can either:

    • create multiple properties and implement them separately just like the one above;
    • change the public HttpPostedFileBase SomeFile property to something like public List<HttpPostedFileBase> SomeFiles and then span multiple @Html.TextBoxFor(m => m.SomeFile, new { type = "file" }) controls to have them all within that list.

    In case you need additional info check out this blog post that I wrote on the topic.

    0 讨论(0)
提交回复
热议问题