Including File Upload in Razor Form View

2019-06-09 10:12发布

问题:

MVC 3, VB.NET. I have a form in my app that gets basic information from the user and then allows them to upload a resume. I am accomplishing this right now by using a redirect to a different form that's only purpose is to select the file for uploading and then submit it.. Sloppy in my opinion.. I have tried to do it from the same form but the file is being lost on submit.. While I am not certain of it I believe it is because I am not using the correct syntax in the form to handle the file... Below is the View so far and then the controller post function..

@ModelType xxxxxxxx.courseproposal
@Code
ViewData("Title") = "Course Proposal"
End Code
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">         </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
 <table>
    <tr>
        <th>Presenter 1</th>
    </tr>

    <tr>
        <td>First Name</td>
        <td>Last Name</td>
        <td>Title</td>
        <td>Phone Number</td>
        <td>Email Address</td>

    </tr>
    <tr>
        <th>@Html.EditorFor(Function(model) model.Name_First1) @Html.ValidationMessageFor(Function(model) model.Name_First1)</th>
        <th>@Html.EditorFor(Function(model) model.Name_Last1) @Html.ValidationMessageFor(Function(model) model.Name_Last1)</th>
        <th>@Html.EditorFor(Function(model) model.Title_1) @Html.ValidationMessageFor(Function(model) model.Title_1)</th>
        <th>@Html.EditorFor(Function(model) model.phone_number1) @Html.ValidationMessageFor(Function(model) model.phone_number1)</th>
        <th>@Html.EditorFor(Function(model) model.email_address1) @Html.ValidationMessageFor(Function(model) model.email_address1)</th>
         <th>@Html.Label("file","Filename:")<input type="file" name="file" id="file" />  </th>
    </tr>
</table> 

<div id="sidebar3">

 <p>
 <input type="submit" value="Submit Course Proposal" />
 </p>

 <p>
 @Html.ActionLink("Back to List", "Index")
 </p>
 </div>

</fieldset>
End Using

And the post Function is like this:

   <AcceptVerbs(HttpVerbs.Post)>
    Function CourseProposal(ByVal courseprop As courseproposal) As ActionResult
        courseprop.conf_Number = _AnnualNumber
        db.courseproposals.AddObject(courseprop)
        db.SaveChanges()
        _id = courseprop.idCourseProposal
        Dim _filename As String = String.Empty
        For Each File As String In Request.Files
            Dim hpf As HttpPostedFileBase = TryCast(Request.Files(File), HttpPostedFileBase)
            If hpf.ContentLength = 0 Then
                Continue For
            End If
            Dim savedfileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CoursePropResumes\" + Path.GetFileName(hpf.FileName)
            hpf.SaveAs(savedfileName)
            _filename = hpf.FileName
        Next

回答1:

Your form needs enctype="multipart/form-data".

Try

Html.BeginForm(null, null, FormMethod.Post, new { enctype="multipart/form-data"})

EDIT: That of course is for C#. In VB it should read

Html.BeginForm(Nothing, Nothing, FormMethod.Post, New With { .enctype="multipart/form-data"})