Пример #1
0
        public static string UploadPhoto(HttpRequestBase Request, int userId, int?adId = null)
        {
            var result = "";

            if (SM.CurrentUserIsNull)
            {
                return("Для загрузки файлов необходимо авторизоваться на сайте");
            }
            foreach (string upload in Request.Files)
            {
                var uploadFile = Request.Files[upload];
                if (uploadFile == null || uploadFile.ContentLength == 0)
                {
                    continue;
                }

                string filename  = uploadFile.FileName;
                var    extension = Path.GetExtension(filename);
                if (extension != null)
                {
                    extension = extension.Replace(".", "");
                }

                var deniedExtensions = new List <string>()
                {
                    "exe", "js"
                };
                if (deniedExtensions.Contains(extension))
                {
                    return("Запрещено загружать файлы с расширением " + extension);
                }

                var isImage = new List <string>()
                {
                    "jpg", "jpeg", "png"
                }.Contains(extension);
                #region обработка изображения
                //var resizedPhoto = PhotosController.GetResizedPhoto(uploadFile.InputStream, 130, 130);

                Image inputImage = new Bitmap(uploadFile.InputStream);
                var   photo      = new Photo()
                {
                    UserId         = userId,
                    AdId           = adId,
                    IsMain         = false,
                    ModerateResult = ModerateResults.NotChecked,
                    Added          = DateTime.Now.ToUniversalTime(),
                    Changed        = DateTime.Now.ToUniversalTime(),
                    Width          = inputImage.Width,
                    Height         = inputImage.Height,
                    PhotoType      = (adId != null ? PhotoTypes.AdPhoto : PhotoTypes.CompanyLogo)
                };

                var folder = PhotosController.GetFileDirectoryForPhotos(photo);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                try
                {
                    //uploadFile.SaveAs(photo.FullPath);
                    var photos = PhotosController.AddPhoto(inputImage, photo);
                    if (photos.Any())
                    {
                        result += "ok," + photos.FirstOrDefault().Id + "," + photo.Url + "|";
                    }
                }
                catch (Exception ex)
                {
                    result = "error";
                    LogsDAL.AddError("in UserController.UploadPhoto(): userId = " + photo.UserId + ", exception: " + ex);
                    return(result);
                }
                #endregion

                /*if (falseisImage)
                 * {
                 #region обработка изображения
                 *  var resizedPhoto = PhotosController.GetResizedPhoto(uploadFile.InputStream, 130, 130);
                 *
                 *  var fileName = "main-" + Guid.NewGuid() + extension;
                 *
                 *  var photo = new Photo()
                 *  {
                 *      UserId = SM.CurrentUserId,
                 *      FileNameWithExtension = fileName,
                 *      IsMain = true,
                 *      ModerateResult = ModerateResults.NotChecked,
                 *      Added = DateTime.Now,
                 *      Changed = DateTime.Now,
                 *      Width = resizedPhoto.Width,
                 *      Height = resizedPhoto.Height
                 *  };
                 *
                 *  bool result = true;
                 *  try
                 *  {
                 *      //uploadFile.SaveAs(photo.FullPath);
                 *      resizedPhoto.Save(photo.FullPath);
                 *  }
                 *  catch (Exception ex)
                 *  {
                 *      result = false;
                 *      LogsDAL.AddError("in UserController.UploadPhoto(): userId = " + photo.UserId + ", exception: " + ex);
                 *  }
                 #endregion
                 * }
                 * else
                 * {
                 *  var newFileFullPath = folder + "/" + filename;
                 *  if (System.IO.File.Exists(newFileFullPath))
                 *      return "На сервере уже есть файл с таким названием. Пожалуйста, переименуйте файл и попробуйте снова";
                 *
                 *  uploadFile.SaveAs(folder + "/" + filename);
                 *
                 *  var file = new MyFile()
                 *  {
                 *      NameWithExtension = filename,
                 *      Extension = extension,
                 *      ContainingFolderPath = folder,
                 *      IsAttachToComment = true,
                 *      UploadedUserId = SM.CurrentUserId,
                 *      UploadedDate = DateTime.Now,
                 *      SizeKb = uploadFile.ContentLength / 1000
                 *  };
                 *
                 *  var newFileId = FilesDAL.AddFile(file);
                 *
                 *  return "ok," + newFileId + "," + filename;
                 * }*/
            }

            return(result);
        }