i am having a mvc application.A web project an开发者_如何学编程d 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
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();
}
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);
}
精彩评论