public async Task <IActionResult> UploadPhoto(UploadPhotoModel model) { if (model.uploadedFile != null) { var user = _context.Users.Include("Photos").Where(u => u.Email == User.Identity.Name).First(); // string path = "/Photos/" + "/" + user.Email; // if (!Directory.Exists(path)) // { // Directory.CreateDirectory(path); // } // path = path + "/" + model.uploadedFile.FileName; string path = "/Photos/" + model.uploadedFile.FileName; // сохраняем файл в папку Photos/userEmail/ в каталоге wwwroot using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await model.uploadedFile.CopyToAsync(fileStream); } var photo = new Photo { Name = model.uploadedFile.FileName, Path = path, Description = model.Description }; photo.User = user; user.Photos.Add(photo); _context.Photos.Add(photo); _context.SaveChanges(); } return(RedirectToAction("Index", "Profile")); }
public async Task<UploadPhotoModel> Upload(List<IFormFile> files) { var file = files[0]; UploadPhotoModel imageResponse = await fileUpload.UploadObject(file); fileUpload.InsertImage(imageResponse.FileUrl); return imageResponse; }
public void UploadPhoto(UploadPhotoModel obj) { int flag = 0; Dictionary <string, int> returndata = new Dictionary <string, int>(); returndata.Add("status", 0); if (obj.apiKey == ConfigurationManager.AppSettings["reasonkey"]) { try { #region To Convert string into image // string fullpath = "sjdhsj"; byte[] bytes = Convert.FromBase64String(obj.imageBase64String); string path = ConfigurationManager.AppSettings["agreementimagepath"]; string fullpath = path + "user" + obj.userid.ToString() + ".jpg"; using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bytes))) { if (File.Exists(fullpath)) { File.Delete(fullpath); } // image.Save(@"D:\Temp\output.jpg", ImageFormat.Jpeg); // Or Png image.Save(fullpath, ImageFormat.Jpeg); } #endregion flag = _agentbal.UploadPhoto(obj.userid, obj.Lat, obj.Lang, fullpath); returndata.Remove("status"); returndata.Add("status", flag); // flag = 1; } catch (Exception ex) { returndata.Remove("status"); returndata.Add(ex.Message, 0); LogBal.ErrorLog(this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.Message, obj.userid); } } string json = JsonConvert.SerializeObject(returndata); HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"; HttpContext.Current.Response.Write(json); }
public async Task <IActionResult> Query_Problem(QueriesView queriesView) { if (ModelState.IsValid) { if (queriesView.photos != null) { string folder = "Proofs/"; queriesView.uploadphoto = new List <UploadPhotoModel>(); foreach (var file in queriesView.photos) { var gallery = new UploadPhotoModel { Name = file.FileName, URL = await UploadImage(folder, file) }; queriesView.uploadphoto.Add(gallery); } } string filepath = ""; if (queriesView.textfile != null) { string folderPath = "Description/"; folderPath += Guid.NewGuid().ToString() + "_" + RandomFileName(6); filepath = Path.Combine(_env.WebRootPath, folderPath); filepath += ".txt"; System.IO.File.WriteAllText(filepath, queriesView.textfile); } Querie querie = new Querie(); querie.Id = RandomString(15); querie.CitizenId = HttpContext.Session.GetString("uid"); querie.status = "Req"; querie.Area = queriesView.Area; querie.zipcode = queriesView.zipcode; querie.textfilepath = filepath; querie.title = queriesView.title; querie.Query_Time = DateTime.Now; querie.Citizen = _context.Citizens.FirstOrDefault(czn => czn.Id == HttpContext.Session.GetString("uid")); querie.ProofPhotos = new List <PhotoModel>(); foreach (var file in queriesView.uploadphoto) { querie.ProofPhotos.Add(new PhotoModel() { Name = file.Name, URL = file.URL }); } await _context.queries.AddAsync(querie); await _context.SaveChangesAsync(); return(RedirectToAction("HomePage", "Citizen")); } else { return(View()); } }