Exemplo n.º 1
0
        public FileUploadResult UploadPhoto(IFormFile fileInfo)
        {
            fileInfo.CheckArgumentIsNull();
            var result = new FileUploadResult {
                StartDateTime = DateTime.Now
            };

            CheckPhotoExtension(fileInfo.FileName);
            CreateDirIfNotExist(PhotoRootPath);
            result.FileExtension = Path.GetExtension(fileInfo.FileName);
            result.FileType      = getPostFileType(result.FileExtension);
            var filename = GetUniqueFileName(result.Id, fileInfo.FileName);

            result.UploadUrl      = GetUploadUrl(Options.WebConfig.UploadPhotoPath, filename);
            result.TargetFileName = Path.Combine(PhotoRootPath, filename);
            try {
                fileInfo.CopyTo(new FileStream(result.TargetFileName, FileMode.Create));
            }
            catch (UploadFileExtensionNotValidException e) {
                result.Exception    = e;
                result.HasError     = true;
                result.ErrorMessage = AppTextDisplay.UploadFileExtNotValid;
            }
            catch (Exception e) {
                result.Exception    = e;
                result.HasError     = true;
                result.ErrorMessage = AppTextDisplay.FileUploadError;
            }
            finally {
                result.FinishDateTime = DateTime.Now;
            }

            return(result);
        }