コード例 #1
0
        public object Post(PostAvatar request)
        {
            var response = new CommonResponse();

            if (Request.Files.Length == 0)
            {
                throw new HttpError(HttpStatusCode.BadRequest, "NoFile");
            }

            var postedFile = Request.Files[0];

            string fileName = postedFile.FileName;

            string baseAttachmentsPath = AppSettings.Get <string>("Avatar");

            bool   useAttachmentsRelativePath  = false;
            string sUseAttachmentsRelativePath = AppSettings.Get <string>("UseAttachmentsRelativePath");

            if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath))
            {
                useAttachmentsRelativePath = bUseAttachmentsRelativePath;
            }

            string currentPathAttachments;
            string folderName = request.TargetFolder;

            if (!string.IsNullOrWhiteSpace(folderName))
            {
                if (useAttachmentsRelativePath)
                {
                    currentPathAttachments = "~/" + baseAttachmentsPath + folderName + @"/".MapHostAbsolutePath();
                }
                else
                {
                    currentPathAttachments = baseAttachmentsPath + folderName + @"\";
                }

                if (!Directory.Exists(currentPathAttachments))
                {
                    Directory.CreateDirectory(currentPathAttachments);
                }
                else
                {
                    AttachmentsIO.ClearDirectory(currentPathAttachments);
                }
            }
            else
            {
                do
                {
                    DateTime date = DateTime.Now;
                    folderName = date.ToString("yy") + date.Month.ToString("d2") +
                                 date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date);

                    if (useAttachmentsRelativePath)
                    {
                        currentPathAttachments = "~/" + baseAttachmentsPath + folderName.MapHostAbsolutePath();
                    }
                    else
                    {
                        currentPathAttachments = baseAttachmentsPath + folderName;
                    }
                } while (Directory.Exists(currentPathAttachments));
                Directory.CreateDirectory(currentPathAttachments);
                if (useAttachmentsRelativePath)
                {
                    currentPathAttachments += @"/";
                }
                else
                {
                    currentPathAttachments += @"\";
                }
            }

            if (postedFile.ContentLength > 0)
            {
                postedFile.SaveTo(currentPathAttachments + Path.GetFileName(postedFile.FileName));
            }

            var attachmentsResult = AttachmentsIO.getAvatarsFromFolder(folderName, "Avatar");

            response.ErrorThrown         = false;
            response.ResponseDescription = folderName;
            response.Result = attachmentsResult;
            return(response);
        }
コード例 #2
0
        public object avatar()
        {
            CommonResponse response = new CommonResponse();

            try
            {
                if (HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    var postedFile = HttpContext.Current.Request.Files["file"];
                    if (postedFile != null)
                    {
                        string fileName = postedFile.FileName;

                        string baseAttachmentsPath = ConfigurationManager.AppSettings["Avatar"];

                        string currentPathAttachments;
                        string folderName = HttpContext.Current.Request["targetFolder"];
                        if (folderName != null && folderName.Trim() != "")
                        {
                            currentPathAttachments = baseAttachmentsPath + folderName + @"\";
                            if (!Directory.Exists(currentPathAttachments))
                            {
                                Directory.CreateDirectory(currentPathAttachments);
                            }
                            else
                            {
                                AttachmentsIO.ClearDirectory(currentPathAttachments);
                            }
                        }
                        else
                        {
                            do
                            {
                                DateTime date = DateTime.Now;
                                folderName = date.ToString("yy") + date.Month.ToString("d2") +
                                             date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date);
                                currentPathAttachments = baseAttachmentsPath + folderName;
                            } while (Directory.Exists(currentPathAttachments));
                            Directory.CreateDirectory(currentPathAttachments);
                            currentPathAttachments += @"\";
                        }

                        if (postedFile.ContentLength > 0)
                        {
                            postedFile.SaveAs(currentPathAttachments + Path.GetFileName(postedFile.FileName));
                        }

                        List <Avatar> attachmentsResult = AttachmentsIO.getAvatarsFromFolder(folderName, "Avatar");

                        response.ErrorThrown         = false;
                        response.ResponseDescription = folderName;
                        response.Result = attachmentsResult;
                    }
                }
            }
            catch (Exception e)
            {
                response.ErrorThrown         = true;
                response.ResponseDescription = "ERROR: " + e.Message;
                response.Result = e;
            }

            return(response);
        }