1

I'm following this example on the MS website for File Uploads using Razor and C#.

If I have more than one File upload buttons, how would the C# code know which button the uploaded file is from? Based on the button the file was uploaded from, I will be saving files to specific folders.

https://learn.microsoft.com/en-us/aspnet/web-pages/overview/data/working-with-files

@using Microsoft.Web.Helpers; @{ var fileName = ""; if (IsPost) { var fileSavePath = ""; var uploadedFile = Request.Files[0]; fileName = Path.GetFileName(uploadedFile.FileName); fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" + fileName); uploadedFile.SaveAs(fileSavePath); } } <!DOCTYPE html> <html> <head> <title>FileUpload - Single-File Example</title> </head> <body> <h1>FileUpload - Single-File Example</h1> @FileUpload.GetHtml( initialNumberOfFiles:1, allowMoreFilesToBeAdded:false, includeFormTag:true, uploadText:"Upload") @if (IsPost) { <span>File uploaded!</span><br/> } </body> </html> 

2 Answers 2

3

The way to do this is to name the buttons the same, but give them different values. You can then do a case statement and direct the logic based on the value.

Razor

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="FirstUpload" /> <input type="submit" name="submitID" id="submitID" value="Upload1" /> <input type="file" name="SecondUpload" /> <input type="submit" name="submitID" id="submitID" value="Upload2" /> } 

Controller

public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(FormCollection collection) { string btn = Request.Params["submitID"]; switch (btn) { case "Upload1": for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; } break; case "Upload2": for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; } break; } return View(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar problem and ended up with following code:
Razor

//BeginForm and other staff @foreach (var d in Model.Types) { <input type="file" name="doc:@d.Id:upload" />//@d.Id is the key point <button formenctype="multipart/form-data" type="submit" formaction="/MyController/uploaddoc" name="typeid" formmethod="post" value="@d.Id">//this way I send Id to controller Upload </button> } 

MyController

[HttpPost] [ValidateAntiForgeryToken] [Route("mycontroller/uploaddoc")]//same as formaction public async Task<ActionResult> UploadDoc(FormCollection data, int typeid) { //restore inpput name var fl = Request.Files["doc:" + typeid.ToString() + ":upload"]; if (fl != null && fl.ContentLength > 0) { var path = Server.MapPath("~/app_data/docs"); var fn = Guid.NewGuid().ToString();//random name using (FileStream fs = System.IO.File.Create(Path.Combine(path, fn))) { await fl.InputStream.CopyToAsync(fs); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.