Asp.net MVC 2 Request.files[""] return 始终为空

Asp.net MVC 2 Request.files[""] return null always

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Movies.Models.StudentModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    
   <form id="form1" runat="server">    
    <script type="text/javascript">
     $(document).ready(function () {
        //$('.date').datepicker({ dateFormat: "dd/mm/yy", Date: Date });
        $('.date').datepicker();
        $('.date').datepicker("setDate", new Date());
     });    

    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();    
            reader.onload = function (e) {
                $('#UImage').attr('src', e.target.result);
            }    
            reader.readAsDataURL(input.files[0]);
        }
    }    
    $("#imgInp").change(function () {
        readURL(this);
    });
</script>
    <h2>Create</h2>    
    <% using (Html.BeginForm("Create", "Student", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>                
            <div class="editor-label">
                <%: Html.LabelFor(model => model.StudentName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.StudentName) %>
                <%: Html.ValidationMessageFor(model => model.StudentName) %>
            </div>                
            <div class="editor-label">
                <%: Html.LabelFor(model => model.RollNo) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.RollNo) %>
                <%: Html.ValidationMessageFor(model => model.RollNo) %>
            </div>    
            <div class="editor-label">
                <%: Html.LabelFor(model => model.STD) %>
            </div>
            <div class="editor-field">    
                <%: Html.DropDownListFor(model => model.STD, Model.STDs, "Please Select Std", new { @class = "form-control" }) %>
                <%: Html.ValidationMessageFor(model => model.STD)%> 
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Address) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Address)%>
                <%: Html.ValidationMessageFor(model => model.Address)%> 
            </div>    
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Address2) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Address2)%>
                <%: Html.ValidationMessageFor(model => model.Address2)%> 
            </div> 
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Image) %>
            </div>
            <div class="editor-field">    
                   <input type='file' id="File1" name="File1" onchange="readURL(this)" />
                   <img id="UImage" src="#" height="300px" width="300px" alt="your image" /> 
             <%: Html.ValidationMessageFor(model => model.Image)%> 
            </div>                
            <div class="editor-label">
                <%: Html.LabelFor(model => model.City) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.City)%>
                <%: Html.ValidationMessageFor(model => model.City)%> 
            </div> 
            <div class="editor-label">
                <%: Html.LabelFor(model => model.ZIP) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.ZIP)%>
                <%: Html.ValidationMessageFor(model => model.ZIP)%> 
                <br />
                <br />
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.DOB) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.DOB, new { @class = "date" })%>
                <%: Html.ValidationMessageFor(model => model.DOB)%> 
            </div> 
            <p>
                <input type="submit" value="Create" />                   
            </p>
        </fieldset>
    <% } %>    
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
    </form>
</asp:Content>

这是我的 aspx 页面,我在其中使用 jquery 向用户显示上传的图像。

控制器代码就像

[HttpPost]
public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1) 
        {
            Student st = new Student();
            HttpPostedFileBase file;

           file = Request.Files["File1"];///it only access the name of the object of html."file1" is the name of the object

            if (file != null && file.ContentLength > 0)
            {
                byte[] Image=null;
                Image = new byte[file.ContentLength];
                file.InputStream.Read(Image, 0, file.ContentLength);
               StudentEn.Image=Image;
            }
            if (ModelState.IsValid)
            {
                st.Insert(StudentEn);
                return RedirectToAction("Index");
            }
            else
            {
                StudentEn.STDs = getSelectedSTD(GetSTDList());
                return View(StudentEn);
            }
        }       

这是控制器代码。我总是得到 request.files null。 我需要更改什么才能上传图片并将其存储到数据库中。

修改razor代码如下

试试下面的剃须刀代码

<% = Html.BeginForm("Create", "Student", FormMethod.Post, new { enctype = "multipart/form-data" })%>

...............
..............

<% Html.EndForm(); %>

删除 using 并尝试

尝试下面的代码..直接使用 File1 而不分配..它对我有效

[HttpPost]
    public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1) 
            {
                Student st = new Student();
                //HttpPostedFileBase file;

              // file = Request.Files["File1"];///it only access the name of the
                                //object of html."file1" is the name of the object

                if (File1 != null && File1.ContentLength > 0)
                {
                    byte[] Image=null;
                    Image = new byte[File1.ContentLength];
                    File1.InputStream.Read(Image, 0, File1.ContentLength);
                   StudentEn.Image=Image;
                }
                if (ModelState.IsValid)
                {
                    st.Insert(StudentEn);
                    return RedirectToAction("Index");
                }
                else
                {
                    StudentEn.STDs = getSelectedSTD(GetSTDList());
                    return View(StudentEn);
                }
            } 

试试这个:

public ActionResult Create(StudentModel StudentEn,FormCollection formCollection) 
{
      HttpPostedFileBase file= formCollection.Get("File1");
}

问题是您的页面中有两个表单,这令人困惑。

尝试removing/Commenting第一种形式

<form id="form1" runat="server">