From your code, I think you want to realize a function that can upload multiple files and show success or error, but there are some problems in your code.
Actually, if you want to post data by beginform, beginform can't receive data form controller and show result. even though writing ajax. Ajax will not work in beginform, that is why the "submit" button doesn't work but "upload" button can click and post data.
If you want to post data by ajax without beginform, the controller will not get the instance of object.
To realize the function, I recommend using "ViewBag". Here‘s the code.
I removed your js and used Viewbag to show result about uploading.
Your controller:
public ActionResult ImageToPDF_Index()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Index2(IEnumerable<HttpPostedFileBase> postedFile)
{
//List<string> errorfiles = new List<string>();
List<string> results = new List<string>(); //it used to store every result of file
//var flag = 0;
string errormassage = "";
foreach (var postfile in postedFile)
{
string filename = postfile.FileName; //get the name of file you upload
string fileExtension = Path.GetExtension(filename); //File extension,it is unnecessary,you can delete it if you wany
try
{
string filePath = Server.MapPath("~/Uploads"); //This is the directory to which you want to upload the file if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);//create the directory
}
postfile.SaveAs(Path.Combine(filePath, filename));
results.Add(filename+"-success"); //upload success
}
catch (Exception e)
{
// flag = 1;
errormassage = e.Message;
// errorfiles.Add(filename);//If there is an exception in the processed file, how to record this file and display it on the View
results.Add(filename + "errormassage");
}
}
ViewBag.result = results; //all result of files
return View("ImageToPDF_Index");
}
Viewbag will transfer the result of every file.
I have tested it to upload many images at once and success.
From your code,I think you want to realize a function that can upload multiple files and show success or error, but there are some problems in your code.
Actually, if you want to post data by beginform,beginform can't receive data from controller and show result.,even though writing ajax.Ajax will not work in beginform, that is why the "submit" button doesn't work but "upload" button can click and post data.
If you want to post data by ajax without beginform, the controller will not get the instance of object.
To realize the function, I recommend using "ViewBag" ,here's the result.
I removed your js and used Viewbag to show result about uploading.
Controller
public ActionResult ImageToPDF_Index()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Index2(IEnumerable<HttpPostedFileBase> postedFile)
{
//List<string> errorfiles = new List<string>();
List<string> results = new List<string>(); //it used to store every result of file
//var flag = 0;
string errormassage = "";
foreach (var postfile in postedFile)
{
string filename = postfile.FileName; //get the name of file you upload
string fileExtension = Path.GetExtension(filename); //File extension,it is unnecessary,you can delete it if you wany
try
{
string filePath = Server.MapPath("~/Uploads"); //This is the directory to which you want to upload the file if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);//create the directory
}
postfile.SaveAs(Path.Combine(filePath, filename));
results.Add(filename+"-success"); //upload success
}
catch (Exception e)
{
// flag = 1;
errormassage = e.Message;
// errorfiles.Add(filename);//If there is an exception in the processed file, how to record this file and display it on the View
results.Add(filename + "errormassage");
}
}
ViewBag.result = results; //all result of files
return View("ImageToPDF_Index");
}
Viewbag will transfer the result of every file.
I have tested it to upload many images at once and success.
Member
5 Points
20 Posts
how to get and show error message from controller to view
May 17, 2020 11:43 PM|ttprettycoder|LINK
like the code:
View:
controller:
If there are other ways to achieve the same purpose, it is also very welcome.
Thanks.
Member
61 Points
21 Posts
Re: how to get and show error message from controller to view
May 19, 2020 02:39 AM|Bruce12138|LINK
Hi , ttprettycoder
From your code, I think you want to realize a function that can upload multiple files and show success or error, but there are some problems in your code.
Actually, if you want to post data by beginform, beginform can't receive data form controller and show result. even though writing ajax. Ajax will not work in beginform, that is why the "submit" button doesn't work but "upload" button can click and post data.
If you want to post data by ajax without beginform, the controller will not get the instance of object.
To realize the function, I recommend using "ViewBag". Here‘s the code.
Your view:
I removed your js and used Viewbag to show result about uploading.
Your controller:
Viewbag will transfer the result of every file.
I have tested it to upload many images at once and success.
Best regards.
Bruce12138
Member
61 Points
21 Posts
Re: how to get and show error message from controller to view
May 19, 2020 03:00 AM|Bruce12138|LINK
Hi ,ttprettycoder
From your code,I think you want to realize a function that can upload multiple files and show success or error, but there are some problems in your code.
Actually, if you want to post data by beginform,beginform can't receive data from controller and show result.,even though writing ajax.Ajax will not work in beginform, that is why the "submit" button doesn't work but "upload" button can click and post data.
If you want to post data by ajax without beginform, the controller will not get the instance of object.
To realize the function, I recommend using "ViewBag" ,here's the result.
View
I removed your js and used Viewbag to show result about uploading.
Controller
Viewbag will transfer the result of every file.
I have tested it to upload many images at once and success.
Best regards.
Bruce12138