コード例 #1
0
ファイル: JobController.cs プロジェクト: chankeye/NiHonGo
        public ActionResult Photo(string id)
        {
            var isSavedSuccessfully = true;
            var fName            = "";
            var defaultPhotoName = "temp" + GetOperation().UserId;
            var photoName        = defaultPhotoName;
            var checkResult      = new IsSuccessResult();

            try
            {
                photoName   = int.Parse(id).ToString();
                checkResult = UserLogic.IsJobMaster(GetOperation().UserId, int.Parse(id));
            }
            catch (Exception)
            {
            }

            if (checkResult.IsSuccess == false)
            {
                isSavedSuccessfully = false;
            }
            else
            {
                foreach (string fileName in Request.Files)
                {
                    var file = Request.Files[fileName];
                    fName = file.FileName;
                    if (file != null && file.ContentLength > 0)
                    {
                        var originalDirectory = new DirectoryInfo(string.Format("{0}MediaUpload\\", Server.MapPath(@"\")));
                        var pathString        = Path.Combine(originalDirectory.ToString(), "JobPhoto");
                        var isExists          = Directory.Exists(pathString);

                        if (!isExists)
                        {
                            Directory.CreateDirectory(pathString);
                        }

                        var ext       = Path.GetExtension(file.FileName);
                        var fileName1 = photoName + ext;
                        var path      = string.Format("{0}\\{1}", pathString, fileName1);
                        file.SaveAs(path);

                        if (photoName.Contains(defaultPhotoName) == false)
                        {
                            var result = JobLogic.UpdatePhoto(int.Parse(id), fileName1);
                            if (result.IsSuccess == false)
                            {
                                isSavedSuccessfully = false;
                            }
                        }
                    }
                }
            }

            if (isSavedSuccessfully)
            {
                return(Json(new { Message = fName }));
            }
            else
            {
                return(Json(new { Message = "Error in saving file" }));
            }
        }