0
返回null

我已經檢查了這個答案不同,但它們都沒有工作對我來說,我在.cshtml代碼如下:HTTPPostedFileBase總是控制器

<input type="file" name="PostedNRLFile" /> 

,然後在控制器我有

public JsonResult SaveRecord(NewAuditLetterViewModel viewModel, FormCollection all, string hvalue, HttpPostedFileBase PostedNRLFile) 

哪個總是空,請在這幫我。

我已經嘗試了一些東西,如在viewmodel中創建一個屬性,這也是null。 還在我的beginform標籤中使用了new { enctype = "multipart/form-data", id = "documentForm" }。 另外檢查它們只是源代碼中的一個標籤。

+0

在您的視圖中顯示'BeginForm()'代碼。 –

+0

@using(Html.BeginForm(「SaveRecord」,「EditNewAuditLetter」,FormMethod.Post,new {enctype =「multipart/form-data」})) –

+0

您應該編輯您的問題!但是你顯示的東西可以正常工作。我假設你正在做一個正常的提交而不使用ajax? –

回答

4

您必須在表單標籤中添加enctype = "multipart/form-data"

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, 
           new { enctype = "multipart/form-data" })) 

試試這個C#代碼在控制器動作

if (Request.Files != null && Request.Files.Count > 0) 
{ 
    HttpPostedFileBase file = Request.Files[0]; 
    if (file != null && file.ContentLength > 0) 
    { 
    } 
} 

你要檢索文件。

+0

OP有'HttpPostedFileBase PostedNRLFile'作爲方法中的參數,所以if(Request.Files!= null && Request.Files.Count> 0){..}'是不必要的 –

0

試試這個:

如果你想使用MVC上傳您需要的文件
var fileCount = Request.Files.Count; 
if (fileCount > 0) 
{ 
for (int i = 0; i < (fileCount); i++) 
    { 
     HttpPostedFileBase Yourfile= Request.Files[i] as HttpPostedFileBase; 
     // do whatever with your file 
    } 
} 
+0

如果使用參數'HttpPostedFileBase PostedNRLFile'不起作用,那麼這肯定會通過。 –

0

: - 創建形式和需要設置標籤:ENCTYPE = 「的multipart/form-data的」 - 創建輸入與相同的名稱控制器中的HttpPostedFileBase

相關問題