1

我已经为此检查了各种答案,但它们都不适合我,我在 .cshtml 中的代码如下:

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

然后在控制器中我有

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

这总是空的,请帮助我。

我已经尝试过一些东西,比如在视图模型中创建一个属性,这也是空的。也用于 new { enctype = "multipart/form-data", id = "documentForm" }我的 beginform 标签。还检查了它们在源代码中只有一个标签。

4

3 回答 3

6

您必须添加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)
    {
    }
}

您必须检索文件。

于 2015-08-26T05:59:17.653 回答
1

如果你想使用 MVC 上传你需要的文件: - 创建表单并且必须设置标签:enctype = "multipart/form-data" - 在控制器中创建名称相同的输入 HttpPostedFileBase

于 2016-06-17T20:05:20.173 回答
0

试试这个:

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
    }
}
于 2015-08-26T06:05:26.843 回答