2017-10-28 96 views
0

在客戶端,我有以下代碼:上傳多個文件時,爲什麼HttpPostedFileBase的列表爲空?

<form action="@Url.Action("UploadStatistics")" method="POST" enctype="multipart/form-data"> 
<h4>Upload Statistics Excel file(s)</h4> 
<p> 
    <input type="file" name="file" id="file" multiple/> 
</p> 

<input type="submit" onclick="ClearDirtyFlag();" /> 

因此,用戶可以選擇多個文件進行上傳。

在服務器端我有這樣的代碼:

public ViewResult UploadStatistics(List<HttpPostedFileBase> files) 
{ 
    //the issue is that the files parameter comes null... 
} 

注意:如果我不希望HttpPostedFileBase對象列表(只是只有HttpPostedFileBase參數)時,代碼工作得很好...

任何人都可以告訴我這裏有什麼問題嗎?

此致敬禮。

+1

'name =「file」'不匹配y我們的參數名稱(複數)。將其更改爲'name =「files」'(或將參數設置爲'List 文件') –

+0

@StephenMuecke謝謝你,它完美地工作。如果你願意,請寫下這個答案,以便我能接受它,也許它會幫助別人。 –

回答

3

您的輸入有name="file"但您的POST方法中的參數名爲files - 它們不匹配。改變輸入到

<input type="file" name="files" id="file" multiple/> 

或更好,有一個視圖模型與屬性

public IEnumerable<HttpPostedFileBase> Files { get; set; } 

,並強烈地結合使用

@Html.TextBoxFor(m => m.Files, new { type = "file" }) 

它給你的是額外的好處模型能夠應用驗證屬性並獲得客戶端和服務器端驗證

相關問題