2
I would like to upload text files where I select N files. txt and saved in a directory I specified in the application; I made an example that was on the internet but 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");
}
Model
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" />
}
I recommend using this plugin, it is very good https://blueimp.github.io/jQuery-File-Upload/
– Tiedt Tech