Skip to content

Commit

Permalink
Comment by Andi on uploading-files-with-aspnetmvc-aspx
Browse files Browse the repository at this point in the history
  • Loading branch information
Andi committed Dec 4, 2023
1 parent 538e3b4 commit 640432f
Showing 1 changed file with 5 additions and 0 deletions.
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"

0 comments on commit 640432f

Please sign in to comment.