Exemple #1
0
        public void UploadPhoto()
        {
            if (Request.Files.Count > 0)
            {
                var upload = Request.Files[0];
                if (upload != null)
                {
                    int    id       = Convert.ToInt32(Request.Form.GetValues("Id")[0]);
                    string fileName = Path.GetFileNameWithoutExtension(upload.FileName) + DateTime.Now.ToString("_yyyy-MM-dd hh-mm-ss") + Path.GetExtension(upload.FileName);
                    byte[] content;

                    using (var reader = new BinaryReader(upload.InputStream))
                    {
                        content = reader.ReadBytes(upload.ContentLength);
                    }

                    using (EmployeeEntities context = new EmployeeEntities())
                    {
                        EmployeeModel em = new EmployeeModel(context);
                        System.IO.File.WriteAllBytes(EmployeeModel.PhotoFilePath + "\\" + fileName, content);

                        em.AddPicture(id, fileName);
                    }
                }
            }
        }