예제 #1
0
        public ActionResult ProfilePhoto(IFormFile file)
        {
            User usr = _helperService.GetUser();

            if (usr == null)
            {
                return(Ok(new ErrorDto {
                    StatusCode = StatusCodes.Status401Unauthorized, Message = "Unauthorized"
                }));
            }
            else
            {
                try
                {
                    string fileName = "profimg.png";
                    if (file.Length > 0)
                    {
                        //fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        FileInfo fi = new FileInfo(fileName);
                        if (fi.Extension.ToLower() == ".jpg" || fi.Extension.ToLower() == ".png" || fi.Extension.ToLower() == ".bmp" || fi.Extension.ToLower() == ".gif")
                        {
                            string fileN    = "profimg.png";
                            string folder   = _helperService.GetFilePath(usr.UserGuid.ToString());
                            string dr       = Directory.GetCurrentDirectory();
                            string savePath = Path.Combine(Directory.GetParent(dr).Parent.ToString(), folder);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string fullPath = Path.Combine(savePath, fileN);

                            Image image    = Image.FromStream(file.OpenReadStream(), true, true);
                            var   newImage = new Bitmap(300, 300);
                            using (var g = Graphics.FromImage(newImage))
                            {
                                g.DrawImage(image, 0, 0, 300, 300);
                                newImage.Save(fullPath, ImageFormat.Png);
                            }
                        }
                        else
                        {
                            return(Ok(new { StatusCode = StatusCodes.Status200OK, message = "Unsuported file format.", result = fileName }));
                        }
                    }
                    else
                    {
                        _helperService.GenAvatar(usr.UserGuid.ToString(), usr.FullName);
                        return(Ok(new { StatusCode = StatusCodes.Status200OK, message = "Remove Successfull.", result = fileName }));
                    }
                    return(Ok(new { StatusCode = StatusCodes.Status200OK, message = "Update Successfull.", result = fileName }));
                }
                catch (System.Exception ex)
                {
                    return(Ok(new { StatusCode = StatusCodes.Status417ExpectationFailed, message = ex.Message }));
                }
            }
        }
예제 #2
0
        public async Task <IActionResult> Answer(IFormFile file, [FromForm] AnswerDTO answer)
        {
            User user = _helperService.GetUser();

            if (user == null)
            {
                return(Ok(new ErrorDto {
                    StatusCode = StatusCodes.Status401Unauthorized, Message = "Unauthorized"
                }));
            }
            else
            {
                try
                {
                    if (file.Length > 0)
                    {
                        string   fileName = ""; fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        FileInfo fi       = new FileInfo(fileName);
                        string   fileN    = _helperService.RandomString(8, true) + fi.Extension;
                        string   strurl   = _helperService.GetMediaUrl(user.UserGuid.ToString()) + "/" + fileN;
                        string   fullPath = Path.Combine(_helperService.GetFilePath(user.UserGuid.ToString()), fileN);
                        using (var stream = new FileStream(fullPath, FileMode.Create))
                        {
                            file.CopyTo(stream);
                        }
                        var acc = await _context.AssesmentCandidate.Where(x => (x.AssessmentId == answer.assessmentid) && (x.QuestionId == answer.questionid)).SingleOrDefaultAsync();

                        AssesmentCandidate ac = new AssesmentCandidate();
                        if (acc == null)
                        {
                            ac.Videofile    = fileN;
                            ac.UploadedOn   = DateTime.Now;
                            ac.AssessmentId = answer.assessmentid;
                            ac.QuestionId   = answer.questionid;
                            ac.Status       = 2;
                            _context.Add(ac);
                        }
                        else
                        {
                            _context.Entry <AssesmentCandidate>(acc).State = EntityState.Detached;
                            ac.Id = acc.Id;
                            _context.AssesmentCandidate.Attach(ac);
                            ac.Videofile    = fileN;
                            ac.AssessmentId = answer.assessmentid;
                            ac.QuestionId   = answer.questionid;
                            ac.UploadedOn   = DateTime.Now;
                            ac.Status       = 2;
                        }
                        await _context.SaveChangesAsync();

                        return(await Question(answer.questionid, answer.assessmentid));
                    }
                    else
                    {
                        return(Ok(new { StatusCode = StatusCodes.Status200OK, message = "Error Submitting data.", result = "" }));
                    }
                }
                catch (System.Exception ex)
                {
                    return(Ok(new { StatusCode = StatusCodes.Status417ExpectationFailed, message = ex.Message }));
                }
            }
        }