2012-05-21 100 views
2

我想上傳文件。我用剃刀使用MVC3。我有以下視圖模型:使用MVC3上傳文件

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。我究竟做錯了什麼?我看過類似的問題,但沒有爲我工作。我迷路了......

回答

2

您的HttpPostedFileBase file方法添加參數,如:

<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 
+0

我沒有完全按照你說和'file'仍然是每一個文件'沒有什麼'。另外'Request.Files'仍然是空的......任何想法爲什麼發生這種情況?我用小提琴手,我沒有看到任何文件正在傳輸... –

+0

好的。我知道發生了什麼事。我使用javascript ajax調用來發布表單,我忘了它。如果你想用ajax發佈文件,你需要做一些技巧。學過的知識。我接受你的答案,因爲它向我保證我的代碼是好的,而其他東西一定會導致意外的行爲。 –