Wednesday, April 25, 2012

ASP.NET MVC Ajax Form: Is enctype correct? Why doesn't file get uploaded?

In the case that the user doesn't have Javascript activated, in order to draw a form, I begin this way:



<% using (Html.BeginForm("Create", "Language", FormMethod.Post,
new {enctype="multipart/form-data"}))
{ %>


If the user has Javascript activated, the following code is used:



<% using (Ajax.BeginForm("Create", "Language",
new AjaxOptions { UpdateTargetId = "CommonArea" },
new { enctype = "multipart/form-data" }))
{ %>


The problem is this:



In the first case, I can get the file uploaded using the following instruction in the business layer:



// Get the uploaded file
HttpPostedFile Flag = HttpContext.Current.Request.Files["Flag"];


In the second case, this instruction doesn't work. How do I know upload that file using the Ajax.BeginForm? Is the code right? Can anyone more experience advise about using jQuery plug-in to upload file before the form submission?



Thank you





No comments:

Post a Comment