Answered by:
error during the code

Question
-
User-1634604574 posted
i have this code
controller
public ActionResult Main4() { string savePath = ConfigurationManager.AppSettings["DocumentationLocation"]; savePath = System.IO.Path.Combine(savePath, "a"); string[] files = Directory.GetFiles(savePath); List<string> downloads = new List<string>(); foreach (string file in files) { downloads.Add(Path.GetFileName("Koala.jpg")); } return View(downloads); } [HttpPost] public ActionResult ProcessForm(List<string> selectedfiles) { if (System.IO.File.Exists(Server.MapPath ("~/zipfiles/bundle.zip"))) { System.IO.File.Delete(Server.MapPath ("~/zipfiles/bundle.zip")); } ZipArchive zip = System.IO.Compression.ZipFile.Open(Server.MapPath ("~/zipfiles/bundle.zip"), ZipArchiveMode.Create); string savePath = ConfigurationManager.AppSettings["DocumentationLocation"]; savePath = System.IO.Path.Combine(savePath, "a"); foreach (string file in selectedfiles) { // zip.CreateEntryFromFile(Server.MapPath("~/images/" + file), file); zip.CreateEntryFromFile((savePath + file), file); } zip.Dispose(); return File(Server.MapPath("~/zipfiles/bundle.zip"), "application/zip", "Satya.zip"); }
view
@{ Layout = null; } @model List<string> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Main4</title> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <link href="~/bootstrapp/css/bootstrap.css" rel="stylesheet" /> <link href="~/bootstrapp/css/bootstrap-theme.css" rel="stylesheet" /> <link href="~/bootstrapp/css/bootstrap-theme.min.css" rel="stylesheet" /> <link href="~/Script_require/sweetalert.css" rel="stylesheet" /> <script src="~/Script_require/sweetalert.js"></script> </head> <body> <div align="center"> <table align="center" border="1" cellpadding="4" cellspacing="4"> <thead> <tr> <th style="background-color: Yellow;color: blue">Select</th> <th style="background-color: Yellow;color: blue">File Name</th> </tr> </thead> @using (Html.BeginForm("ProcessForm", "Main", FormMethod.Post, new { @enctype = "multipart/form-data" })) { <tr> <td> <input type="submit" id="fileUploadExcel2" onclick="return validateData2();" class="button button4" value="Download2" /> </td> <td> <input type="text" checked id="fileUpload" name="selectedfiles" value="Koala.jpg" /> </td> </tr> } </table> </div> <script> function validateData2() { sweetAlert("Congratulations!!", "Your file is downloaded", "success"); return true; } //code for validation using checkbox function validateData() { if ($('[type="checkbox"]').is(':checked')) { sweetAlert("Congratulations!!", "Your file is downloaded", "success"); return true; } //else if ($('[type="checkbox"]').not(':checked')) { // sweetAlert("Selection Was Empty!!", "Please select at least one", "error"); // return false; //} else { sweetAlert("Selection Was Empty!!", "Please select at least one", "error"); return false; } } </script> </body> </html>
i get this error (POST http://localhost:83/Main/ProcessForm 500 (Internal Server Error)) when i run it on iis
TB: this image Koala.jpg inside sub-folder a which is that sub-folder is inside folder temp in local c
Sunday, May 24, 2020 5:31 PM
Answers
-
User753101303 posted
Note the a prefix ie it fails on trying to get
C:\temp\aKoala.jpg
Are you 100% sure this file exists? What is the purpose of this "a" prefix? Or it is supposed to be a folder and you want c:\temp\a\Koala.jpg?
Another option is that this error comes from an earlier design issue. "Koala" is hardcoded in your code. You wanted rather to show files found in a directory?
Edit: maybe you wanted also Server.IO.Path.Combine(savePath,file) rarther than savePath+file?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 25, 2020 8:17 AM -
User634495354 posted
I will provide you with two solutions to the problem.
First , adding "/" in "zip.CreateEntryFromFile((savePath + file), file)".
foreach (string file in selectedfiles) { // zip.CreateEntryFromFile(Server.MapPath // ("~/images/" + file), file); zip.CreateEntryFromFile((savePath +"/"+ file), file); }
Second , it similar to the first way, adding "/" in "savePath = System.IO.Path.Combine(savePath, "a");"
savePath = System.IO.Path.Combine(savePath, "a/");
You combine the "savePath" and "a", so the url of the "savePath" in your pc is "C:\temp\a", when the code " zip.CreateEntryFromFile((savePath + file), file);" is executed, it attaches the image which will be downloaded, savePath+file is "C:\temp\a" +"koala.jpg" and become "C:\temp\akoala.jpg". So it can't find image.
Maybe I didn’t explain it clearly, if you don’t understand it, I can explain it with the code again.
Best regards
Bruce12138
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 25, 2020 8:35 AM
All replies
-
User475983607 posted
I recommend basic troubleshooting and exception handling. Add try...catch block to log exceptions. Often 500 error are logged in the event Viewer. Open the event View and take a look.
My best guess is the application pool identity has not be granted read/write access to the root folder. I can only guess given thr limited view of your design and code.
Sunday, May 24, 2020 5:52 PM -
User-1634604574 posted
i used this demo https://github.com/satyaCsgithub/first
but i just changed server.map to
string savePath = ConfigurationManager.AppSettings["DocumentationLocation"]; savePath = System.IO.Path.Combine(savePath, "a");
i get that error
Sunday, May 24, 2020 6:04 PM -
User475983607 posted
Your response is puzzling. Did you check the event view for errors? Did you add basic exception handling and logging to find the error? Can you explain the troubleshooting steps you have performed up to this point?
I verified the demo code in your other similar thread, works when deployed to IIS. If the folder is outside the application root then the IIS application pool needs read/write access to the directory.
Sunday, May 24, 2020 6:55 PM -
User-1634604574 posted
how can i do it if you know plz write the sample
Sunday, May 24, 2020 7:02 PM -
User634495354 posted
Hi,zhyanadil.it@gmail.com
I tested it and realized the function of reading and downloading images. so there's no problem about your code.
First , I want to know what error the controller specifically reported,many problems may lead to 500 in browser.
Second , Can you show me your Web.config? I want to see the part about <appsetting>,especially the key is "DocumentationLocation".
Last , you need to ensure koala. jpg is not only in the "a" folder, but also in the "images" folder of this project, so that no error will be reported when downloading.
Best regards
Bruce12138
Monday, May 25, 2020 3:18 AM -
User753101303 posted
Hi,
See https://stackify.com/beyond-iis-logs-find-failed-iis-asp-net-requests/ and point 3.
http 500 means you have a server side exception which is written by default to the event viewer. Once then it makes sense to llok at the code knowing what to look for.
Trying to guess from the code is usually much longer and beyond obvious issues is just a guess. For exampple one could wonder if a folder exists or permissions are ok but there is no way to be sure about that by just reading code.
In short always start from an actual error message.
Monday, May 25, 2020 7:44 AM -
User-1634604574 posted
here is my web.config
<appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="aspnet:MaxJsonDeserializerMembers" value="150000" /> <add key="DocumentationLocation" value="C:\temp" /> </appSettings>
i have koala. jpg inside folder images
here is my error
Could not find file 'C:\temp\aKoala.jpg'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\temp\aKoala.jpg'. Source Error: Line 66: savePath = System.IO.Path.Combine(savePath, "a"); Line 67: Line 68: foreach (string file in selectedfiles)
Monday, May 25, 2020 7:48 AM -
User753101303 posted
Note the a prefix ie it fails on trying to get
C:\temp\aKoala.jpg
Are you 100% sure this file exists? What is the purpose of this "a" prefix? Or it is supposed to be a folder and you want c:\temp\a\Koala.jpg?
Another option is that this error comes from an earlier design issue. "Koala" is hardcoded in your code. You wanted rather to show files found in a directory?
Edit: maybe you wanted also Server.IO.Path.Combine(savePath,file) rarther than savePath+file?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 25, 2020 8:17 AM -
User634495354 posted
I will provide you with two solutions to the problem.
First , adding "/" in "zip.CreateEntryFromFile((savePath + file), file)".
foreach (string file in selectedfiles) { // zip.CreateEntryFromFile(Server.MapPath // ("~/images/" + file), file); zip.CreateEntryFromFile((savePath +"/"+ file), file); }
Second , it similar to the first way, adding "/" in "savePath = System.IO.Path.Combine(savePath, "a");"
savePath = System.IO.Path.Combine(savePath, "a/");
You combine the "savePath" and "a", so the url of the "savePath" in your pc is "C:\temp\a", when the code " zip.CreateEntryFromFile((savePath + file), file);" is executed, it attaches the image which will be downloaded, savePath+file is "C:\temp\a" +"koala.jpg" and become "C:\temp\akoala.jpg". So it can't find image.
Maybe I didn’t explain it clearly, if you don’t understand it, I can explain it with the code again.
Best regards
Bruce12138
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 25, 2020 8:35 AM