Uploading file: the .NET MVC way
In ASP.NET, the traditional way of uploading files is as follows.
<form id=”form1″ runat=”server”>
<asp:FileUpload ID=”FileUpload1″ runat=”server” />
<asp:Button ID=”Button1″ runat=”server” Text=”Upload”
<asp:Label ID=”Label1″ runat=”server”></asp:Label></p>
And you have access to the uploaded file like this.
protected void Button1_Click(object sender, EventArgs e)
In .NET MVC, you can achieve the same with the following code.
<form action=”/controller/action/” accept-charset=”UNKNOWN” enctype=”multipart/form-data” method=”post”>
<input id=”file1″ name=”file1″ size=”20″ type=”file” />
The controller side
foreach (string fileName in Request.Files)
HttpPostedFile file = Request.Files[fileName ];
It is important to understand the fundamental difference between the traditional web-form based .NET programming methods and the .NET MVC methods. You can still use the server controls, but you can only use them for read-only data rendering purpose, since ViewState is no longer an option in .NET MVC.