asp.net-mvc-3 - 使用MVC3上传文件

标签 asp.net-mvc-3 file-upload viewmodel

我想上传文件。我将 MVC3 与 Razor 一起使用。我有以下 ViewModel:

Public Class ImportL2EViewModel
    <Required(AllowEmptyStrings:=False, ErrorMessage:="L2E name required")>
    Public Property Name As String

    Public Property File As HttpPostedFileBase    
End Class

在我看来,我创建了一个表单:

@Using Html.BeginForm("Import", "L2ECreationWizard", FormMethod.Post, New Dictionary(Of String, Object) From {{"enctype", "multipart/form-data"}})
    @<div class="welcome-box acenter"> 
        <div style="display: block; text-align: left; width: 330px; margin: auto;">
            <div class="property">
                @Html.LabelFor(Function(m) m.Name)
                @Html.TextBoxFor(Function(m) m.Name)
            </div> 
            <div class="property">
                @Html.LabelFor(Function(m) m.File)
                @Html.TextBoxFor(Function(m) m.File, New With {.type = "file"})
            </div>            
        </div>
        <div class="actionBar">
            <a class="import fright button" href="#">Import</a>
        </div>
    </div>
End Using

生成的 html 如下所示:

<form method="post" enctype="multipart/form-data" action="/L2ECreationWizard/Import" novalidate="novalidate">
    <div class="welcome-box acenter"> 
        <div style="display: block; text-align: left; width: 330px; margin: auto;">
            <div class="property">
                <label for="Name">Name</label>
                <input type="text" value="" name="Name" id="Name" data-val-required="L2E name required" data-val="true">
            </div> 
            <div class="property">
                <label for="File">File</label>
                <input type="file" value="" name="File" id="File">
            </div>            
        </div>
        <div class="actionBar">
            <a href="#" class="import fright button">Import</a>
        </div>
    </div>
</form>

我将表单发布到以下操作方法:

<HttpPost()>
Function Import(vm As ImportL2EViewModel) As ActionResult
    ' Nothing yet
End Function

发布后,我可以看到 vm.Name 已填写,但 vm.FileNothingRequest.Files.Count0。我究竟做错了什么?我在 SO 上看到过类似的问题,但对我来说没有任何作用。我迷路了...

最佳答案

HttpPostedFileBase 文件 的方法添加一个参数,如下所示:

<HttpPost()>
Function Import(vm As ImportL2EViewModel, file As HttpPostedFileBase) As ActionResult

Dim fileName = Path.GetFileName(file.FileName)
    Dim physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName)

    // The files are not actually saved in this demo
    // file.SaveAs(physicalPath)
    ...
End Function

并从模型中删除 HttpPostedFileBase 属性。它是 Request 对象的一部分,因此无法添加到您的模型中。

如果您允许选择多个文件,那么您需要能够循环遍历每个上传的文件

<HttpPost()>
Function Import(vm As ImportL2EViewModel, attachments As IEnumerable(Of HttpPostedFileBase)) As ActionResult

    For Each file As var In attachments
        Dim fileName = Path.GetFileName(file.FileName)
        Dim physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName)
        ' The files are not actually saved in this demo
        ' file.SaveAs(physicalPath);
    Next
    ...
End Function

关于asp.net-mvc-3 - 使用MVC3上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10687209/

相关文章:

silverlight - 使用 Caliburn.Micro 将 SelectionChanged 绑定(bind)到 ViewModel

swift - 使用选择器时如何处理回调?

asp.net-mvc - 使用 ASP.NET MVC Razor 的自定义控件

c# - @Html.HiddenFor 不适用于 ASP.NET MVC 中的列表

spring-boot - Spring Webflux 上传大图片文件并以流方式使用 WebClient 发送文件

c# - asp.net mvc 中的 HttpPostedFileBase 状态

c# - 仅支持初始值设定项、实体成员和实体导航属性。 (ASP.NET MVC 和 Entity Framework )

javascript 警报消息未在 mvc3 中显示

ruby-on-rails - Rails file_field_tag 不上传文件

asp.net-mvc - 流畅的验证、域和 View 模型