public async Task <IActionResult> AddUserDocumentsById([FromRoute] long userid, IList <IFormFile> models) { User usr = _repo.Find(userid); if (usr == null) { return(BadRequest("Invalid User")); } IList <string> filepaths = new List <string>(); IList <UserDocument> docs = new List <UserDocument>(); foreach (IFormFile f in models) { string storePath = userid.ToString() + "_" + f.Name; string docFolder = Path.Combine("UserDocuments", storePath); try { if (f == null || f.Length == 0) { return(Content("File not selected")); } string path = FileUploadHandler.GetFilePathForUpload(docFolder, f.FileName); using (FileStream stream = new FileStream(path, FileMode.Create)) { await f.CopyToAsync(stream); } } catch (Exception e) { return(Json(new { result = "Upload Failed", error = e.Message })); } string filePath = FileUploadHandler.FileReturnPath(docFolder, f.FileName); UserDocument doc = new UserDocument { UserId = userid, FilePath = filePath, DocumentName = f.FileName }; docs.Add(doc); filepaths.Add(filePath); } doc_repo.AddRange(docs); return(Json(new { FilePaths = filepaths, UserID = userid })); }
public async Task <IActionResult> AddPatientDocuments([FromRoute] long patientid, List <IFormFile> models) { IList <string> filepaths = new List <string>(); IList <PatientDocument> docs = new List <PatientDocument>(); foreach (IFormFile f in models) { string storePath = patientid.ToString() + "_" + f.Name; string docFolder = Path.Combine("PatientDocuments", storePath); try { if (f == null || f.Length == 0) { return(Content("File not selected")); } string path = FileUploadHandler.GetFilePathForUpload(docFolder, f.FileName); using (FileStream stream = new FileStream(path, FileMode.Create)) { await f.CopyToAsync(stream); } } catch (Exception e) { return(Json(new { result = "Upload Failed", error = e.Message })); } string filePath = FileUploadHandler.FileReturnPath(docFolder, f.FileName); PatientDocument doc = new PatientDocument { PatientId = patientid, FilePath = filePath, DocumentName = f.FileName }; docs.Add(doc); filepaths.Add(filePath); } doc_repo.AddRange(docs); return(Json(new { FilePaths = filepaths, PatientID = patientid })); }
public IActionResult AddUserPhotoById([FromRoute] long userid, IFormFile file) { User mod = _repo.Find(userid); if (mod == null) { return(NotFound("Incorrect UserId")); } string userPath = userid.ToString() + "_Photo_" + file.FileName; string docFolder = Path.Combine("EmployeesImages", userPath); try { if (file == null || file.Length == 0) { return(Content("File Not Selected")); } string path = FileUploadHandler.GetFilePathForUpload(docFolder, file.FileName); using (FileStream stream = new FileStream(path, FileMode.Create)) { file.CopyTo(stream); } } catch (Exception e) { return(Json(new { result = "upload Failed", error = e.Message })); } string filePath = FileUploadHandler.FileReturnPath(docFolder, file.FileName); mod.UserPhoto = new UserPhoto { FilePath = filePath, UserId = userid }; _repo.Update(mod); return(new OkObjectResult(new { UserPhotoID = mod.UserPhotoId })); }
public async Task <ActionResult> UploadStockImage(IFormFile file, long OutletStockId) { // full path to file in temp location var stockObj = Repo.GetStockItemById(OutletStockId); if (stockObj == null) { return(new NotFoundResult()); } var filePath = ""; string storePath = "Store_" + stockObj.StoreVisit.StoreId; string docFolder = Path.Combine("StoreImages", storePath, "Stock"); try { if (file == null || file.Length == 0) { return(Content("file not selected")); } var path = FileUploadHandler.GetFilePathForUpload(docFolder, file.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } } catch (Exception e) { return(Json(new { result = "upload Failed", error = e.Message })); } filePath = FileUploadHandler.FileReturnPath(docFolder, file.FileName); stockObj.ImageUrl = filePath; Repo.UpdateStock(stockObj); return(Json(new { filepath = filePath, OutletStockId = OutletStockId })); }
public async Task <ActionResult> UploadUserImage(IFormFile file, long id) { // full path to file in temp location var filePath = ""; string storePath = "User_" + id.ToString(); string docFolder = Path.Combine("ProfileImages", storePath); try { if (file == null || file.Length == 0) { return(Content("file not selected")); } var path = FileUploadHandler.GetFilePathForUpload(docFolder, file.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } } catch (Exception e) { return(Json(new { result = "upload Failed", error = e.Message })); } filePath = FileUploadHandler.FileReturnPath(docFolder, file.FileName); var user = _repo.Find(id); if (user != null) { user.PhotoFilePath = filePath; _repo.Update(user); } return(Json(new { filepath = filePath, userid = id })); }