예제 #1
0
        public JsonResult Insert_Attachment(object obj)
        {
            AjaxViewModel aViewModel = new AjaxViewModel();

            try
            {
                AjaxManager aMan = new AjaxManager();

                var length = Request.ContentLength;
                var bytes  = new byte[length];
                Request.InputStream.Read(bytes, 0, length);
                // bytes has byte content here. what do do next?

                var fileName = Request.Headers["X-File-Name"];
                var fileSize = Request.Headers["X-File-Size"];
                var fileType = Request.Headers["X-File-Type"];
                var ref_Id   = Request.Headers["RefId"];
                var ref_Type = Request.Headers["RefType"];
                var remark   = Request.Headers["Remark"];


                aViewModel.Attachment.Document_Name = fileName;

                aViewModel.Attachment.Ref_Type = Convert.ToInt32(ref_Type);

                aViewModel.Attachment.Ref_Id = Convert.ToInt32(ref_Id);

                aViewModel.Attachment.CreatedBy = ((UserInfo)Session["User"]).UserId;

                aViewModel.Attachment.UpdatedBy = ((UserInfo)Session["User"]).UserId;

                aViewModel.Attachment.CreatedOn = DateTime.Now;

                aViewModel.Attachment.UpdatedOn = DateTime.Now;

                aViewModel.Attachment.Remark = remark;

                var saveToFileLoc = string.Format("{0}\\{1}",
                                                  Server.MapPath("/uploads/" + aViewModel.Attachment.Ref_Type_Str + "/" + aViewModel.Attachment.Ref_Id),
                                                  fileName);

                bool directoryExists = System.IO.Directory.Exists(Server.MapPath("/uploads/" + aViewModel.Attachment.Ref_Type_Str + "/" + aViewModel.Attachment.Ref_Id));

                if (!directoryExists)
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("/uploads/" + aViewModel.Attachment.Ref_Type_Str + "/" + aViewModel.Attachment.Ref_Id));
                }

                bool FileExists = System.IO.File.Exists(Server.MapPath("/uploads/" + aViewModel.Attachment.Ref_Type_Str + "/" + aViewModel.Attachment.Ref_Id + "/" + fileName));

                if (!FileExists)
                {
                    // save the file.
                    var fileStream = new FileStream(saveToFileLoc, FileMode.Create, FileAccess.ReadWrite);

                    fileStream.Write(bytes, 0, length);

                    fileStream.Close();

                    aViewModel.Attachment.Attachment_Id = aMan.Insert_Attachment(aViewModel.Attachment);

                    aViewModel.Friendly_Message.Add(MessageStore.Get("AJ001"));
                }
                else
                {
                    aViewModel.Friendly_Message.Add(MessageStore.Get("AJ003"));
                }

                // aViewModel.Attachments = aMan.Get_Attachments_By_Ref_Type_Ref_Id(aViewModel.Attachment.Ref_Type, aViewModel.Attachment.Ref_Id);
            }
            catch (Exception ex)
            {
                aViewModel.Friendly_Message.Add(MessageStore.Get("SYS01"));

                Logger.Error("Ajax Controller - Insert_Attachments " + ex.ToString());
            }

            return(Json(aViewModel, JsonRequestBehavior.AllowGet));
        }