public ActionResult Create(DataViewModel postModel, FormCollection formValues) { DataCreateModel dataCreateModel = CreateModel(postModel, formValues,Operation.Create); DataViewModel model = dataCreateModel.Model; foreach(Field field in model.Form.Fields.Where(m => m.FieldTypeId == 11)) { field.Data = Request.UserHostAddress; } if (ModelState.IsValid) { RepositoryMapguide repositoryMapGuide = new RepositoryMapguide(); model = repositoryMapGuide.Add(model); dataCreateModel.Model = model; repositoryMapGuide.SendEmails(dataCreateModel); Models.View endView = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 7); if (endView == null) { Models.View listUser = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 6); if (listUser == null) { Models.View listAll = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 5); if (listAll == null) { return View(model.View.Name, model); } else { return RedirectToAction("Index", new { id = listAll.ViewId, formId = model.Form.FormId }); } } else { return RedirectToAction("Index", new { id = listUser.ViewId, formId = model.Form.FormId }); } } else { ViewBag.Id = model.View.ViewId; model.View = endView; return View(endView.Name, model); } } else return View(model.View.Name, model); }
//public async Task<HttpResponseMessage> PostFormData() public Task<HttpResponseMessage> PostFormData() { string root = HttpContext.Current.Server.MapPath("~/App_Data"); /*StreamWriter log1; string file1 = Path.Combine(@"d:\inetpub\wwwroot\rapportfrastedet\app_data\", "log1.txt"); if (!File.Exists(file1)) { log1 = new StreamWriter(file1); } else { log1 = File.AppendText(file1); } // Write to the file: log1.WriteLine(DateTime.Now); log1.WriteLine(root); // Close the stream: log1.Close(); */ // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new MultipartFormDataStreamProvider(root); /**/ // Read the form data. //await Request.Content.ReadAsMultipartAsync(provider); return Request.Content.ReadAsMultipartAsync(provider).ContinueWith(data => {//.Wait(); // This illustrates how to get the file names. /*foreach (MultipartFileData file in provider.FileData) { Trace.WriteLine(file.Headers.ContentDisposition.FileName); Trace.WriteLine("Server file path: " + file.LocalFileName); }*/ try { HttpContextWrapper http = (HttpContextWrapper)Request.Properties["MS_HttpContext"]; HttpContext.Current = http.ApplicationInstance.Context; RepositorySaveData r = new RepositorySaveData(); DataCreateModel dataCreateModel = r.CreateModel(data.Result.FormData, data.Result.FileData, Operation.Create); DataViewModel model = dataCreateModel.Model; foreach (Field field in model.Form.Fields) { if (field.FieldTypeId == 11) field.Data = http.Request.UserHostAddress; } RepositoryMapguide repositoryMapGuide = new RepositoryMapguide(); model = repositoryMapGuide.Add(model); dataCreateModel.Model = model; repositoryMapGuide.SendEmails(dataCreateModel); return Request.CreateResponse(HttpStatusCode.OK); } catch (System.Exception e) { StreamWriter log; string file = Path.Combine(root, "log.txt"); if (!File.Exists(file)) { log = new StreamWriter(file); } else { log = File.AppendText(file); } // Write to the file: log.WriteLine(DateTime.Now); log.WriteLine(e.Message); log.WriteLine(e.StackTrace); // Close the stream: log.Close(); HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); System.Net.Http.Headers.MediaTypeWithQualityHeaderValue json = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"); System.Net.Http.Headers.MediaTypeWithQualityHeaderValue plain = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/plain"); if (!Request.Headers.Accept.Contains(json)) { r.Content.Headers.ContentType = plain; } return r; } });