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)); }