Exemplo n.º 1
0
        private void uploadWholeFile(HttpContext requestContext, IList <ViewDataUploadFilesResult> statuses, Guid tempRequestKey, long?forignKeyId)
        {
            var request = requestContext.Request;

            for (int i = 0; i < request.Files.Count; i++)
            {
                var            file = request.Files[i];
                IFileUploadDTO f    = fileUploadService.GetNew();

                f.Name           = file.FileName;
                f.Size           = file.ContentLength;
                f.TempRequestKey = tempRequestKey;
                f.Type           = file.ContentType;
                f.ForignKeyId    = forignKeyId;
                using (var binaryReader = new BinaryReader(file.InputStream))
                {
                    f.Body = binaryReader.ReadBytes(file.ContentLength);
                }

                if (neadThumbnailFileExt.Contains(Path.GetExtension(f.Name)))
                {
                    f.Thumbnail = new WebImage(f.Body).Resize(80, 80).GetBytes();
                }


                long id = fileUploadService.SaveFile(f);

                statuses.Add(uploadResult(id, f.Name, f.Size, f.Type));
            }
        }
Exemplo n.º 2
0
        public long SaveFile(IFileUploadDTO dto)
        {
            lock (lockObj)
            {
                Settings settings = settingsService.Get();

                int c = requestFileRepository.GetList(
                    new RequestFileByNameAndForignKeyOrTempKeySpecification(dto.Name, dto.ForignKeyId, dto.TempRequestKey))
                        .Count();

                if (c > 0)
                {
                    return(0);
                }


                c = requestFileRepository.Count(t => t.RequestId != null && t.RequestId == dto.ForignKeyId ||
                                                t.TempRequestKey != null && t.TempRequestKey == dto.TempRequestKey);

                if (c >= settings.MaxRequestFileCount)
                {
                    throw new DataServiceException(String.Format(Resource.MaxRequestFileCountConstraintMsg, settings.MaxRequestFileCount));
                }

                if (dto.Size / 1024 >= settings.MaxRequestFileSize)
                {
                    throw new DataServiceException(String.Format(Resource.MaxRequestFileSizeConstraintMsg, settings.MaxRequestFileSize, dto.Name));
                }

                if (String.IsNullOrWhiteSpace(dto.Name))
                {
                    throw new DataServiceException(Resource.EmptyFileNameConstraintMsg);
                }

                if (dto.Name.Length > settings.MaxFileNameLength)
                {
                    throw new DataServiceException(String.Format(Resource.MaxFileNameConstraintMsg, settings.MaxFileNameLength, dto.Name));
                }

                if (dto.Body == null || dto.Body.Length == 0)
                {
                    throw new DataServiceException(Resource.EmptyFileBodyConstraintMsg);
                }

                RequestFile file = new RequestFile()
                {
                    Body           = dto.Body,
                    Name           = dto.Name,
                    RequestId      = dto.ForignKeyId,
                    Size           = dto.Size,
                    TempRequestKey = dto.TempRequestKey,
                    Thumbnail      = dto.Thumbnail,
                    Type           = dto.Type
                };

                requestFileRepository.Save(file);
                repository.SaveChanges();

                return(file.Id);
            }
        }