I would like to upload text files where I select N .txt files and save them to a directory I have specified in the application; I made an example that was on the internet but I could not get the files in my action.
Action:
[HttpPost]
public ActionResult Index(Upload upload)
{
foreach (var file in upload.Files) {
if (file.ContentLength > 0)
{
var filename = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Arquivo"), filename);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
Template
public class Upload
{
public IEnumerable <HttpPostedFile> Files { get; set; }
}
View
<h2>Upload de arquivo</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.Label("File")
<input type="file" name="Files" id="Files" accept=".txt" multiple />
<input type="submit" value="Upload" />
}