コード例 #1
0
        public ActionResult UploadPhoto(HttpPostedFileBase file, MemberContext mem)
        {
            string serverPath = "";
            string filePath   = "";

            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    //useful, but actually not used here. I eventually figured out changing permissions on the folder
                    //this returned mfreedm
                    string user = HttpContext.User.Identity.Name;

                    serverPath = "/Platform/Images/" + file.FileName;
                    filePath   = Server.MapPath(serverPath);
                    file.SaveAs(filePath);
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }

            var unitOfWork = new GenericRepository.UnitOfWork(mem);

            MemberDatabase.File photo = new MemberDatabase.File();

            photo.FilePath    = serverPath;
            photo.FileType    = "Photo";
            photo.UploadDate  = DateTime.Now;
            photo.Description = "uploaded document";

            unitOfWork.Repository <MemberDatabase.File>().Insert(photo);
            unitOfWork.Save();


            List <File> fileList = unitOfWork.Repository <MemberDatabase.File>().Query().Get().ToList();

            return(View("Index", fileList));
        }
コード例 #2
0
        public ActionResult UploadDocument(HttpPostedFileBase file)
        {
            string        connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            MemberContext mem        = new MemberContext(connection);
            var           unitOfWork = new GenericRepository.UnitOfWork(mem);

            if (file != null && file.ContentLength > 0)
            {
                string path = "C:\\Users\\mfreedm\\Documents\\Visual Studio 2015\\Projects\\Platform\\Platform\\Temp";
                try
                {
                    file.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }


                MemberDatabase.File newFile = new MemberDatabase.File();

                newFile.FilePath    = path;
                newFile.Description = "File Uploaded";

                unitOfWork.Repository <File>().Insert(newFile);
                unitOfWork.Save();
            }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }

            List <File> fileList = unitOfWork.Repository <File>().Query().Get().ToList();

            //problems with losing viewbag after using redirectToaction
            return(View("Documents", fileList));
        }