Example #1
0
        public IActionResult Upload(IFormFile[] file, Guid?id) //Dropzone içerisinde tanımlı kelıme file
        {
            if (id.HasValue)
            {
                Post post = _postRepository.GetById(id.Value);
                if (post == null)
                {
                    return(NotFound());
                }

                if (file.Length > 0)
                {
                    foreach (var item in file)
                    {
                        var result = _fileUpload.Upload(item);
                        if (result.FileResult == Utility.FileResult.Succeded)
                        {
                            PostImage image = new PostImage
                            {
                                ImageUrl = result.FileUrl,
                                PostId   = id.Value
                            };

                            _postImageRepository.Add(image);
                            _postImageRepository.Save();
                        }
                    }
                }
            }
            return(View());
        }
Example #2
0
        public async Task <IActionResult> Upload(IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest("File is null"));
            }

            if (file.Length == 0)
            {
                return(BadRequest("File is empty"));
            }

            if (file.Length > 10 * 1024 * 1024)
            {
                return(BadRequest("File size cannot exceed 10M"));
            }

            var acceptTypes = new[] { ".jpg", ".jpeg", ".png" };

            if (acceptTypes.All(t => t != Path.GetExtension(file.FileName).ToLower()))
            {
                return(BadRequest("File type not valid, only jpg and png are acceptable."));
            }

            if (string.IsNullOrWhiteSpace(_hostingEnvironment.WebRootPath))
            {
                _hostingEnvironment.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
            }

            var uploadsFolderPath = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");

            if (!Directory.Exists(uploadsFolderPath))
            {
                Directory.CreateDirectory(uploadsFolderPath);
            }

            var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var postImage = new PostImage
            {
                FileName = fileName
            };

            _postImageRepository.Add(postImage);

            await _unitOfWork.SaveAsync();

            var result = _mapper.Map <PostImage, PostImageResource>(postImage);

            return(Ok(result));
        }
        public async Task <IActionResult> Upload(IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest("文件为空"));
            }

            if (file.Length == 0)
            {
                return(BadRequest("文件大小为0"));
            }

            if (file.Length > 10 * 1024 * 1024)
            {
                return(BadRequest("文件不能超过10M"));
            }

            var acceptTypes = new[] { ".jpg", ".jpeg", ".png" };

            if (acceptTypes.All(t => t != Path.GetExtension(file.FileName).ToLower()))
            {
                return(BadRequest("文件格式不支持,只支持.jpg .jpeg .png格式的文件"));
            }

            if (string.IsNullOrWhiteSpace(hostingEnvironment.WebRootPath))
            {
                hostingEnvironment.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
            }

            var uploadsFolderPath = Path.Combine(hostingEnvironment.WebRootPath, "uploads");

            if (!Directory.Exists(uploadsFolderPath))
            {
                Directory.CreateDirectory(uploadsFolderPath);
            }

            var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var postImage = new PostImage
            {
                FileName = fileName
            };

            postImageRepository.Add(postImage);

            await unitOfWork.SaveAsync();

            var result = mapper.Map <PostImage, PostImageResource>(postImage);

            return(Ok(result));
        }
 public void Add(PostImage postImage)
 {
     _postImageRepository.Add(postImage);
 }