Exemplo n.º 1
0
        public DTO_CHLD_StudentImage AddStudentImage(DTO_Image request)
        {
            byte[] imageBytes = Convert.FromBase64String(request.ImageBytes);

            Stream fileStream = new MemoryStream(imageBytes);

            using (var context = new DB_connection())
            {
                var student = context.CORE_Students.FirstOrDefault(s => s.StudentID == request.OwnerID);

                if (student == null)
                {
                    return(null);
                }

                var result = new DTO_CHLD_StudentImage
                {
                    ImageName = request.ImageName,
                    ImageURL  = FTPURLBASE,
                    ImageUUID = Guid.NewGuid().ToString(),
                    StudentID = request.OwnerID
                };

                try
                {
                    var ftpRequest = (FtpWebRequest)WebRequest.Create(result.ImageURL + result.ImageUUID + result.ImageName);
                    ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

                    ftpRequest.Credentials = new NetworkCredential("jumpcree_gigaloan", "gigaloan");

                    using (Stream ftpStream = ftpRequest.GetRequestStream())
                    {
                        int    length    = 1024;
                        byte[] buffer    = new byte[length];
                        int    bytesRead = 0;

                        do
                        {
                            bytesRead = fileStream.Read(buffer, 0, length);
                            ftpStream.Write(buffer, 0, bytesRead);
                        } while (bytesRead != 0);
                    }
                }
                catch (WebException e)
                {
                    return(null);
                }

                result.ImageURL += "images/"; //long story....

                context.proc_AddStudentImage(result.StudentID, result.ImageURL, result.ImageUUID, result.ImageName);
                context.SaveChanges();

                return(result);
            }
        }