I want to upload files in an asp .net mvc application. The upload itself is easy to do but I want to upload them with other details. In fact I'm working on a tool for projects managing, every file should correspond to a task i.e every user is able to upload files that correspond to one task of his. So, i just want to upload the file with the id of the task and then save the name of the file and the id of the task in database. There should be a drop down list for the tasks.
1 Answer
From my understanding you want to upload file with other details. First you have to start creating a view model that contaons HttpPostedFileBase to capture the uploaded file and other properties to capture other details.
public class UploadFileViewModel
{
public HttpPostedFileBase File { get; set; }
public int TaskId { get; set; }
// other properties if any
}
Basically you should have an action that have the UploadFileViewModel as parameter.
public ActionResult UploadFile(UploadFileViewModel model)
{
...
}
I would suggest you to go for a view model because you can easily apply validations. Without a view model also you could do something like this in action,
public ActionResult UploadFile(HttpPostedFileBase file, int taskId)
{
...
}
Your view should be something like this,
@model UploadFileViewModel
@using (Html.BeginForm("UploadFile", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="file" />
@Html.DropDownListFor() @* list the tasks *@
<input type="submit" value="OK" />
</div>
}
If you want to know more information about uploading files check this blog post.