How to keep input type=file field value after failed validation in ASP.NET MVC?

前端 未结 7 1825
闹比i
闹比i 2020-11-27 04:39

I\'ve got a simple form in an MVC app I\'ve made. It contains a file field so users can upload an image. It all works great.

Problem is, if the form submit fails val

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 05:00

    I fail to agree with "impossible" being marked as correct answer. In case anybody is still in search of a possibility, here is the work around that worked for me. I'm using MVC5. The idea is to use a session variable. I got the idea from ASP.Net Form.

    My Model/ViewModel (only relevant properties):

    public partial class emp_leaves
        {
            public string fileNameOrig { get; set; }
            public byte[] fileContent { get; set; }
    
            public HttpPostedFileBase uploadFile { get; set; }
        }
    

    In my controller (HttpPost): //Check

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(emp_leaves emp_leaves)
    {
        if (emp_leaves.uploadFile != null && emp_leaves.uploadFile.ContentLength>0 && !string.IsNullOrEmpty(emp_leaves.uploadFile.FileName))
        {
            emp_leaves.fileNameOrig = Path.GetFileName(emp_leaves.uploadFile.FileName);
            emp_leaves.fileContent = new byte[emp_leaves.uploadFile.ContentLength];
            emp_leaves.uploadFile.InputStream.Read(emp_leaves.fileContent, 0, emp_leaves.uploadFile.ContentLength);
            Session["emp_leaves.uploadFile"] = emp_leaves.uploadFile; //saving the file in session variable here
        }
        else if (Session["emp_leaves.uploadFile"] != null)
        {//if re-submitting after a failed validation you will reach here.
            emp_leaves.uploadFile = (HttpPostedFileBase)Session["emp_leaves.uploadFile"];
            if (emp_leaves.uploadFile != null && emp_leaves.uploadFile.ContentLength>0 && !string.IsNullOrEmpty(emp_leaves.uploadFile.FileName))
            {
                emp_leaves.fileNameOrig = Path.GetFileName(emp_leaves.uploadFile.FileName);
                emp_leaves.uploadFile.InputStream.Position = 0;
                emp_leaves.fileContent = new byte[emp_leaves.uploadFile.ContentLength];
                emp_leaves.uploadFile.InputStream.Read(emp_leaves.fileContent, 0, emp_leaves.uploadFile.ContentLength);    
            }
        }
    //code to save follows here...
    }
    

    Finally within my edit view: here, i am conditionally showing the file upload control.

    < script type = "text/javascript" >
      $("#removefile").on("click", function(e) {
        if (!confirm('Delete File?')) {
          e.preventDefault();
          return false;
        }
        $('#fileNameOrig').val('');
        //toggle visibility for concerned div
        $('#downloadlrfdiv').hide();
        $('#uploadlrfdiv').show();
        return false;
      }); <
    /script>
    
    @model PPMSWEB.Models.emp_leaves @{ HttpPostedFileBase uploadFileSession = Session["emp_leaves.uploadFile"] == null ? null : (HttpPostedFileBase)Session["emp_leaves.uploadFile"]; } @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data"
    })) { @Html.AntiForgeryToken()
    
    @*irrelevant content removed*@
    @Model.fileNameOrig @if (isEditable && !Model.readonlyMode) { @Html.Raw(" "); }
    @Html.TextBoxFor(model => model.uploadFile, new { @type = "file", @class = "btn btn-default", @title = "Upload file (max 300 KB)" }) @Html.ValidationMessageFor(x => x.uploadFile)
    }

提交回复
热议问题