public GeneralResponse AddToDoResultAttachment(AddToDoResultAttachmentRequest request, Guid CreateEmployeeID) { GeneralResponse response = new GeneralResponse(); try { ToDoResult toDoResult = _toDoResultRepository.FindBy(request.ToDoResultID); #region Rename The file // extract the extention var fileExtention = Path.GetExtension(request.Attachment); // Get directory var directory = Path.GetDirectoryName(request.Attachment); // create filename string fileName = directory + "\\ToDoResult_" + request.ToDoResultID + fileExtention; // Rename file //File.Move(request.Attachment, fileName); #endregion toDoResult.AttachmentType = fileExtention; toDoResult.Attachment = fileName; toDoResult.ModifiedDate = PersianDateTime.Now; toDoResult.ModifiedEmployee = _employeeRepository.FindBy(CreateEmployeeID); _toDoResultRepository.Save(toDoResult); _uow.Commit(); } catch (Exception ex) { response.ErrorMessages.Add(ex.Message); if (ex.InnerException != null) { response.ErrorMessages.Add(ex.InnerException.Message); } } return(response); }
public JsonResult ToDoResult_AttachFile(Guid ToDoResultID, string AttacmentName, HttpPostedFileBase file) { GeneralResponse response = new GeneralResponse(); ToDoResultView toDoResult = _toDoService.GetToDoResult(ToDoResultID); if (toDoResult.Attachment != null) { response.ErrorMessages.Add("برای این مورد قبلا یک فایل ذخیره شده است. برای افزودن فایل جدید ابتدا فایل قبلی را حذف کنید"); return(Json(response, JsonRequestBehavior.AllowGet)); } string fileName = string.Empty; string path = string.Empty; try { #region Upload file // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // extract the extention var fileExtention = Path.GetExtension(file.FileName); // create filename //string fileName = response.ID + "." + fileExtention; // fileName = Path.GetFileName(file.FileName); // Create a unique file name fileName = "ToDoResult_" + ToDoResultID + fileExtention; // Gettin current Year and Month PersianCalendar pc = new PersianCalendar(); int year = pc.GetYear(DateTime.Now); int month = pc.GetMonth(DateTime.Now); // Create File Path path = Path.Combine(Server.MapPath("~/data/ToDoFiles"), fileName); // Create reqired directried if not exist new FileInfo(path).Directory.Create(); // Uploading using (var fs = new FileStream(path, FileMode.Create)) { var buffer = new byte[file.InputStream.Length]; file.InputStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, buffer.Length); } } #endregion } catch (Exception ex) { //response.success = false; response.ErrorMessages.Add("در آپلود کردن فایل خطایی به وجود آمده است."); return(Json(response, JsonRequestBehavior.AllowGet)); } AddToDoResultAttachmentRequest request = new AddToDoResultAttachmentRequest(); request.ToDoResultID = ToDoResultID; request.Attachment = path; request.AttachmentName = AttacmentName; response = _toDoService.AddToDoResultAttachment(request, GetEmployee().ID); return(Json(response, JsonRequestBehavior.AllowGet)); }