I need to implement uploading files to the server and save the path to them in the database. The problem is that I have a lot of tables (about 30) in which the path can be saved, and if you use a single stored procedure, you get a large number of if -s. Therefore, I decided to write a lot of things and call exactly the right one, thanks to which I managed to use the space of ≈90 checks of only ≈30 (when downloading 3 files in the worst cases when the last if) Darkness seems to me that this is some kind of bicycle and most likely there are some better ways to do this, and besides, I don’t like the repetitive code in the second method. How can you better solve some of these problems?

[HttpPost] public JsonResult UploadAjax(string nameJournal) { int count = -1; string[] files = new string[Request.Files.Count]; string relativePath = "~/Files/nameJournal/" + id; var absolutePath = Server.MapPath(relativePath); string resultError = "Файлы не загружены:"; if (!Directory.Exists(absolutePath)) { DirectoryInfo dir = Directory.CreateDirectory(absolutePath); } foreach (string fileNameFromView in Request.Files) { count += 1; HttpPostedFileBase file = Request.Files[fileNameFromView]; string fileName = Path.GetFileNameWithoutExtension(file.FileName) + "_" + DateTime.Now.ToShortDateString() + Path.GetExtension(file.FileName); try { file.SaveAs(absolutePath + "/" + fileName); files[count] = file.FileName; } catch (Exception) { resultError += string.Format("\n{0}", file.FileName); } } return Json(UploadDataBase(files, resultError, relativePath, string nameJournal)); } 

And the second method

  public string UploadDataBase(string[] files, string resultError, string relativePath, nameJournal) { string resultSuccess = "Файлы загружены:"; if (nameJournal == "ТАКОМУ-ТО") { for (int i = 0; i < files.Length; i++) { if (files[i] != null) { try { //Хранимка1 resultSuccess += string.Format("\n{0}", files[i]); } catch (Exception) { System.IO.File.Delete(Server.MapPath(relativePath + "/" + files[i])); resultError += string.Format("\n{0}", files[i]); } } } } else if (nameJournal == "ТАКОМУ-ТО2") { for (int i = 0; i < files.Length; i++) { if (files[i] != null) { try { //Хранимка2 resultSuccess += string.Format("\n{0}", files[i]); } catch (Exception) { System.IO.File.Delete(Server.MapPath(relativePath + "/" + files[i])); resultError += string.Format("\n{0}", files[i]); } } } } return resultSuccess + "\n\n" + resultError; } 
  • Maybe you better create a table in which you'll keep all the paths, as well as a link to the essence of which the path belongs? - igosh

1 answer 1

If the code in the IF is always the same, why not replace the IFs with a Dictionary?

 Dictionary<string, string> dictionary = new Dictionary<string, string>() { {"JournalID1","StoredProcID1"}, {"JournalID2","StoredProcID2"}, } public string UploadDataBase(string[] files, string resultError, string relativePath, string nameJournal) { string resultSuccess = "Файлы загружены:"; if (dictionary.ContainsKey(nameJournal)) { var proc = dictionary[nameJournal]; for (int i = 0; i < files.Length; i++) { if (files[i] != null) { try { //Хранимка1 // use proc here resultSuccess += string.Format("\n{0}", files[i]); } catch (Exception) { System.IO.File.Delete(Server.MapPath(relativePath + "/" + files[i])); resultError += string.Format("\n{0}", files[i]); } } } } else { // Эррор, журнал не найден } return resultSuccess + "\n\n" + resultError; }