Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

.net - ASP.NET MVC3 RAZOR: File Upload gives zero as file count

I need to upload multiple files into web server using MVC3 with RAZOR. I have the following code. In the controller, I am getting zero as the file count. How to correct it to get the actual number of files being uploaded and to get the content?

public class MyFileController : Controller
{

    public ActionResult MyFileProcessActionTest()
    {
        return View();
    }

    [HttpPost]
    public ActionResult MyFileProcessActionTest(IEnumerable<System.Web.HttpPostedFileBase> files)
    {

        int fileCount = files.Count<System.Web.HttpPostedFileBase>();
        return RedirectToAction("Index");
    }
}

VIEW

@{
ViewBag.Title = "MyFileProcessActionTest";
}

<h2>MyFileProcessActionTest</h2>

@using (Html.BeginForm())
{

<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />

<input type="submit"  />

}

READING:

  1. Binding HttpPostedFileBase using Ajax.BeginForm

  2. ASP.NET MVC Uploading and Downloading Files http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files

  3. How do I Validate the File Type of a File Upload?

  4. MVC 3 file upload and model binding

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must include the enctype attribute in the form tag to indicate that the form should include files.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...