Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited tags
Link
tereško
  • 58.5k
  • 26
  • 100
  • 151
edited tags
Link
robrich
  • 13.3k
  • 7
  • 39
  • 63
Source Link

MVC Upload with Postback ( or maybe it is called Callback )

I am using MVC to upload some files. The following code works fine but I would like to be return some information from the server ( eg a message or an ID ). What I am trying to do is very simple but I fear I amy not have made it clear. Can anyone help please?

VIEW

 @using (Html.BeginForm("AddFileX", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="files" multiple="true" /> <input id="submit" type="submit" value="Upload" /> } 

CONTROLLER

 [HttpPost] public ActionResult AddFileX(IEnumerable<HttpPostedFileBase> files) { foreach (var file in files) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); } } // I would like to return a message or an ID of something // (eg "you are about to overwrite your files" or the ID of something not shown here) return View("IdxUpload"); // This line probably needs to be changed }