public async Task <object> UploadFiles() { var ctx = HttpContext.Current; var root = ctx.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); string trainFileString = "trainFile"; string testFileDString = "testFile"; String trainFilePath = "NULL"; String testFilePath = "NULL"; ////string AlgoType11 = HttpContext.Current.Request["type"]; string AlgoType12 = HttpContext.Current.Request.QueryString["type"]; //temp for testing string temppp = provider.FormData.ToString().Trim('=').ToLower(); int index = 0; try { await Request.Content .ReadAsMultipartAsync(provider); globalsModels.num++; foreach (var file in provider.FileData) { index++; if (index > 2) { break; } var name = file.Headers .ContentDisposition .FileName; if (name.Contains(".csv") == false) { return($"Error not CSV file"); } // remove double quotes from string. name = name.Trim('"'); name = AlgoType12 + globalsModels.num + name; var localFileName = file.LocalFileName; var filePath = Path.Combine(root, name); ///if we got file with the same name,we delete the old file and insert the new one if (File.Exists(filePath)) { File.Delete(filePath); } File.Move(localFileName, filePath); ///saving the path to the train and test file for the server if (name.Contains(trainFileString)) { trainFilePath = filePath; } if (name.Contains(testFileDString)) { testFilePath = filePath; } myfiles.Add(filePath); } } catch (Exception e) { return($"Error: {e.Message}"); } string jsonName = "Model_" + globalsModels.num + ".json"; var serverUploadPath = HttpContext.Current.Server.MapPath("~/App_Data/" + jsonName); string AlgoType; if (AlgoType12 != null) { AlgoType = AlgoType12.ToLower(); } else { AlgoType = temppp; } server = new DetectorServer(trainFilePath, testFilePath, AlgoType, serverUploadPath); server.Serialize(); Console.WriteLine(index); Console.WriteLine("done"); object jsonObject = loadAndReturnMyJson(serverUploadPath); return(jsonObject); }
/// post method. the users upload the csv files though the homepage and the function save them in ///the folder "App_Data" in the project location. ////the full path for the files saved in mydocfiles public void PostFromHomePage() { HttpResponseMessage result = null; var httpRequest = HttpContext.Current.Request; int index = 0; ///upload the files if (httpRequest.Files.Count > 0) { System.Console.WriteLine("You received the call!"); string trainFileString = "train"; string testFileDString = "test"; string name; string AlgoType = HttpContext.Current.Request.Form["AlgorithemType"]; /////if submit is pressed without any upload file if (httpRequest.Files[0].FileName == "" || httpRequest.Files[1].FileName == "") { HttpResponseMessage response22 = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "one of the files is missing"); throw new HttpResponseException(response22); ///////>>>>>>>>only for testing .delete later<<<<<<<<< var serverUploadPath1 = HttpContext.Current.Server.MapPath("~/App_Data/" + "Model_1.json"); LoadJson(serverUploadPath1); var response1 = Request.CreateResponse(HttpStatusCode.Moved); response1.Headers.Location = new Uri("http://localhost:8080/"); ///////>>>>>>>>if loop ^^^^^^^^^^ to only for testing .delete later<<<<<<<<<*/ return; } /// String trainFilePath = "NULL"; String testFilePath = "NULL"; globalsModels.num++; foreach (string file in httpRequest.Files) { index++; if (index > 2) { break; } var postedFile = httpRequest.Files[file]; name = postedFile.FileName; if (name.Contains(".csv") == false) { HttpResponseMessage response22 = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "one of the file isnt CSV file"); throw new HttpResponseException(response22); } if (file.Contains(trainFileString)) { name = trainFileString + globalsModels.num + AlgoType + name; var filePath = HttpContext.Current.Server.MapPath("~/App_Data/" + name); postedFile.SaveAs(filePath); bool isEq1 = FilesAreEqual(myfiles, filePath); if (isEq1 == false) { trainFilePath = filePath; ///postedFile.SaveAs(filePath); myfiles.Add(filePath); } if (isEq1 == true) { trainFilePath = "NULL"; } } if (file.Contains(testFileDString)) { name = testFileDString + globalsModels.num + AlgoType + name; var filePath = HttpContext.Current.Server.MapPath("~/App_Data/" + name); postedFile.SaveAs(filePath); bool isEq2 = FilesAreEqual(myfiles, filePath); if (isEq2 == false) { testFilePath = filePath; /// postedFile.SaveAs(filePath); myfiles.Add(filePath); } if (isEq2 == true) { testFilePath = "NULL"; } } } ///if we didnt get the test and train file,we decrease the number of model and throw error messege if ((testFilePath.Equals("NULL")) || (trainFilePath.Equals("NULL"))) { globalsModels.num--; HttpResponseMessage response22 = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "one of the files is missing or use already upload this files"); throw new HttpResponseException(response22); } ////sending the files to the server string jsonName = "Model_" + globalsModels.num + ".json"; var serverUploadPath = HttpContext.Current.Server.MapPath("~/App_Data/" + jsonName); AlgoType = AlgoType.ToLower(); server = new DetectorServer(trainFilePath, testFilePath, AlgoType, serverUploadPath); server.Serialize(); Console.WriteLine("done"); LoadJson(serverUploadPath); /* result = Request.CreateResponse(HttpStatusCode.Created, myfiles); * ///redirecting back to the homepage * var response = Request.CreateResponse(HttpStatusCode.Moved); * response.Headers.Location = new Uri("http://localhost:8080/"); * ///return response;*/ return; } else { //there isnt any file HttpResponseMessage response22 = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "there isnt any file to upload"); throw new HttpResponseException(response22); } return; }