tags:

views:

65

answers:

2

hello all,

i am having a mvc application.A web project and the language i am using is C#.

i am having a update category form and in that there is a file upload control please tell me how will i do the update functionality because in the update controller we usually pass the collections object.

please tell me what will I do..and How will I do.

Thanks Ritz

+2  A: 

Change the enctype of the form element to multipart form-data:

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

Add a file input to this form:

<input type="file" name="filetoupload" id="filetoupload" />

and read the file in your controller action:

public ActionResult Upload()
{
    var uploadedFile = Request.Files["filetoupload"];
    // TODO: do something with the uploaded file
    return View();
}
Darin Dimitrov
+1  A: 

The controller will have a Request property, which has a Files property.

foreach (string name in Request.Files)
{
    HttpPostedFile file = Request.Files[name];

    string filePath = Path.Combine(@"C:\Somewhere", Path.GetFileName(file.FileName));
    file.SaveAs(filePath);
}
Daniel Earwicker