Пример #1
0
        public static long UploadFile(HttpPostedFileBase uploadedFile, UploadedFileCategory category)
        {
            var result = FileHelper.Upload(uploadedFile, category);

            if (!result.Success)
            {
                return(0);
            }

            var uploadedFileService = ServiceLocator.Resolve <IUploadedFileService>();
            var existingFile        = uploadedFileService.GetByPhysicalPath(result.ActualPath);

            var dtoToSave = new UploadedFileDto
            {
                PhysicalPath     = result.ActualPath,
                Category         = category,
                UploadedBy       = SmsCache.UserContext.UserName,
                UploadedDateTime = DateTime.Now
            };

            if (existingFile.Success && existingFile.Data != null)
            {
                dtoToSave.UploadedFileID = existingFile.Data.UploadedFileID;
            }

            var saveResult = uploadedFileService.Save(dtoToSave);

            if (!saveResult.Success || saveResult.Data == null)
            {
                return(0);
            }

            return(saveResult.Data.UploadedFileID);
        }
Пример #2
0
        private static string GetFolderPath(UploadedFileCategory category)
        {
            var dir = Path.Combine(
                SmsCache.SystemInformation[Constant.ConstKey.SystemInfo_FileUploadPath],
                category.ToString());

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return(dir);
        }
Пример #3
0
        public static FileUploadReturn Upload(HttpPostedFileBase uploadedFile, UploadedFileCategory category, bool overwrite = true)
        {
            if (uploadedFile.ContentLength > 0)
            {
                var fileName = Path.GetFileName(uploadedFile.FileName);
                if (fileName == null)
                {
                    return new FileUploadReturn {
                               Success = false
                    }
                }
                ;

                if (category == UploadedFileCategory.ProfileImage)
                {
                    var extension = fileName.Remove(0, fileName.LastIndexOf('.'));

                    fileName = SmsCache.UserContext.UserID + extension;
                    DeleteOldProfileImages();
                }

                var path = GetFilePath(category, fileName);
                if (File.Exists(path))
                {
                    if (overwrite)
                    {
                        File.Delete(path);
                    }
                    else
                    {
                        return new FileUploadReturn {
                                   Success = false
                        }
                    };
                }

                uploadedFile.SaveAs(path);

                return(new FileUploadReturn {
                    Success = true, ActualPath = path
                });
            }
            return(new FileUploadReturn {
                Success = false
            });
        }
Пример #4
0
 private static string GetFilePath(UploadedFileCategory category, string filename)
 {
     return(Path.Combine(GetFolderPath(category), filename));
 }