I want to implement a file uplaod logic in MVC, My view(cshtml) would contain something like this.
<div id="dialog" title="Upload files">
<p><input type="file" id="fileUpload" name="fileUpload" /> </p>
<p><input type="submit" value="Upload file" /></p>
<% } %>
</div>
And my controller would have the logic to save the file.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
How do i link the click event to trigger this controller? This was just a sample code I got in the net, LEt me know if it isn’t right.
Thanks,
Adarsh
You will trigger this controller action by submitting a form, just like the MVC paradigm works.
In the view you should place this HTML in a form like the following:
This will trigger you controller.
I do not know how using the file as a parameter works. I usually use the
Request.Filesproperty in my controller.