-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comment by Andi on uploading-files-with-aspnetmvc-aspx
- Loading branch information
Andi
committed
Dec 4, 2023
1 parent
538e3b4
commit 640432f
Showing
1 changed file
with
5 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
_data/comments/uploading-files-with-aspnetmvc-aspx/4dffca65.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
id: 4dffca65 | ||
date: 2023-12-04T03:37:16.2165285Z | ||
name: Andi | ||
avatar: https://github.com/Keskebeu.png | ||
message: "How about saving it to a database. So lets say an array instead.\r\nI am trying to use:\r\n[HttpPost]\r\n[ValidateAntiForgeryToken]\r\npublic async Task<IActionResult> Create(Bind[\"Binding here\"]Person person)\r\n{\r\n \r\n List<Person> list = new List<Person>();\r\n if (Request.Form.Files.Count > 0)\r\n {\r\n var dataStream = new MemoryStream();\r\n for (var i = 0; i < Request.Form.Files.Count;i++)\r\n {\r\n var files = Request.Form.Files[i];\r\n await files.CopyToAsync(dataStream);\r\n person.Files[i].Name = files.FileName;\r\n person.Files[i].Data = dataStream.ToArray();\r\n }\r\n }\r\n list.Add(person);\r\n return RedirectToAction(nameof(Index));\r\n}\r\nRazor View:\r\n\r\n<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">\r\n \r\n <label for=\"file1\">Filename:</label>\r\n <input type=\"file\" name=\"files\" id=\"file1\" />\r\n \r\n <label>File</label>\r\n for(var i = 0; i < 2; i++)\r\n{\r\n <input type=\"file\" accept=\".pdf\" asp-for=\"Files[i].Data\" class=\"form-control\" \r\n id=\"Files[i].Data\" data-for=\"Files\" />\r\n}\r\n\r\n <input type=\"submit\" />\r\n</form>\r\nYou can mess with the DOM to add more rows of inputs.\r\nI was looking for a more finessed way of one input with multiple files like below\r\n<input type=\"file\" accept=\".pdf\" multiple />\r\nWhen I do I get the error \"index out of range\".\r\nAny thoughts?\r\n\r\n" |