简体   繁体   中英

image upload?

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM