Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Posts Tagged "file upload"

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” />
<p>
<asp:Button ID=”Button1″ runat=”server” Text=”Upload”
OnClick=”Button1_Click” /></p>
<p>
<asp:Label ID=”Label1″ runat=”server”></asp:Label></p>
</form>
And you have access to the uploaded file like this.
protected void Button1_Click(object sender, EventArgs e)
{
//FileUpload1.PostedFile
}
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”>
<label for=”file1″>file1</label>
<input id=”file1″ name=”file1″ size=”20″ type=”file” />
</form>
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.
-SK

\\\