I have a ASP.NET MVC 4 Project, and I need to be able to upload a file or files to save them and make an instance of that file or files( so I will be able to keep information about them).
In order to implement this I have two classes inside of 'Models':
1. UploadFile: (this class will represent a single file)
public class UploadFile
{
public long ID { get; set; }
public string Path { get; set; }
public string Status { get; set; }
public HttpPostedFileBase File { get; set; }
}
2. Scan: (this class will represent a one upload of file or files)
public class Scan
{
public IEnumerable<UploadFile> Files { get; set; }
public long ContentLength()
{
long size = 0;
foreach (UploadFile f in Files)
{
size =+ f.File.ContentLength;
}
return size;
}
}
also I have one controller 'HomeController.cs", and I have this action in there:
[HttpPost]
public ActionResult Index(Scan scan)
{
if (scan.ContentLength() > 0)
{
foreach (var s in scan.Files)
{
var fileName = Path.GetFileName(s.File.FileName);
var path = Path.Combine(Server.MapPath("~/Content/UploadFiles"), fileName);
s.File.SaveAs(path);
}
}
return RedirectToAction("Index");
}
And the View is 'index.cshtml', and this is the begin.form:
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>File:</td>
<td><input type="file" name="Scan" id="Scan" multiple /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Upload" /></td>
</tr>
</table>
}
</div>
My Main Goal: upload a file/files (save them at "~/Content/UploadFiles") and during this process to create an instance of UploadFile and Scan (Scan hold a collection of UploadFile), so i can represent them to the user and keep tracking and catalog them in my DB.
My problem is that "scan" is null in 'homecontroller.cs' how can i pass argument so i could create instances of the two classes and still upload the file ?
As you may notice i'm new at MVC design-structure, so if i'm missing a point or two of MVC because of my implement let me know. Thanks.