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

相关问题