예제 #1
0
        public ActionResult SaveImage(IFormFile imgfile, [FromHeader(Name = "x-requestid")] string request)
        {
            var rule = new IsFileNotNull().And(new IsFileSizeSuitable(_options)).And(new IsFileExtntionSuitable()).And(new IsFileSignatureSuitable());

            if (!rule.IsSatisfiedBy(imgfile))
            {
                return(BadRequest("File size should less than 2Mb, Type should be [JPG, JPEG, PNG] and not empty."));
            }
            bool result = Guid.TryParse(request, out Guid requestId);

            if (!result)
            {
                return(BadRequest("Apartment requestid is required."));
            }
            _picService.UploadFile(new FileData(imgfile, requestId));
            return(Ok("The file went to storage."));
        }
예제 #2
0
        public async Task UploadImage(IFormFile file, Guid requestId)
        {
            if (file == null)
            {
                return;
            }
            var rule = new IsFileNotNull().And(new IsFileSizeSuitable(_options)).And(new IsFileExtntionSuitable()).And(new IsFileSignatureSuitable());

            if (!rule.IsSatisfiedBy(file))
            {
                throw new ArgumentException("Data saved, but file size should less than 2Mb and type should be [JPG, JPEG, PNG].");
            }
            string url     = Uris.GetUrl(_apartmentUrl, "Pic");
            var    imgData = new MultipartFormDataContent
            {
                { new StreamContent(file.OpenReadStream()), "imgfile", file.FileName }
            };

            _httpClient.DefaultRequestHeaders.Add("x-requestid", requestId.ToString());
            var response = await _httpClient.PostAsync(url, imgData);

            _logger.LogDebug("[UploadImage] -> response code {StatusCode}", response.StatusCode);
            response.EnsureSuccessStatusCode();
        }