File Upload in ASP.NET MVC 3: How to Implement Correctly?
Hello everyone! I'm new to ASP.NET MVC 3 and trying to implement file upload to the server. I've read a bunch of articles but still get confused.
I have a form in the View where the user should select a file (e.g., an image or document) and submit it. I did this:
```csharp
// Model
public class UploadModel
{
public string FileName { get; set; }
public HttpPostedFileBase File { get; set; }
}
// Controller
[HttpPost]
public ActionResult Upload(UploadModel model)
{
if (model.File != null && model.File.ContentLength > 0)
{
var fileName = Path.GetFileName(model.File.FileName);
var path = Path.Combine(Server.MapPath("~/uploads"), fileName);
model.File.SaveAs(path);
ViewBag.Message = "File uploaded!";
}
```
No answers yet.
Log in to leave an answer