【问题标题】:Asp.net MVC 2 Request.files[""] return null alwaysAsp.net MVC 2 Request.files[""] 总是返回 null
【发布时间】:2015-05-27 14:48:59
【问题描述】:
<%@ 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 为空。 我需要更改什么才能上传图像并将其存储到数据库中。

【问题讨论】:

  • 方法Create()中,第二个参数HttpPostedFileBase File1是干什么用的?我根本看不到您的代码引用它...
  • 为什么要将 File1 分配给文件..只需使用 File1 而不是文件并检查结果..
  • 试试Request.Files[0]
  • 我建议您不要将 Webforms 与 MVC 混合使用。 (即:&lt;asp:Content)。

标签: c# asp.net-mvc-2 image-upload


【解决方案1】:

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

尝试删除/评论第一个表单

即&lt;form id="form1" runat="server"&gt;

【讨论】:

    【解决方案2】:

    修改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);
                    }
                } 
    

    【讨论】:

    • 我按照@Sachu 的建议尝试了它不起作用,但它使 File1 为空。
    • 然后根据@erikphilip 尽量避免使用带有MVC 的webforms..使用mvc 语法..因为相同的代码对我来说适用于MVC razor 格式
    • 我在这个项目中使用了 MVC 2,所以使用了 webforms 语法。
    • 我已经按照你的建议更改了aspx文件,但结果还是一样。
    【解决方案3】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2016-04-08
      • 2012-01-23
      • 2019-01-20
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 2016-05-12
      相关资源
      最近更新 更多