Example #1
0
        public async Task <PlaceImageResponse> UploadImage(MultipartFormDataContent item)
        {
            //var content = System.Text.Json.JsonSerializer.Serialize(item);
            //var bodyContent = new StringContent(content, Encoding.UTF8, "application/json");

            ////var postResult = await _client.PostAsync("/api/placeinfo", bodyContent);


            ////var postContent = await postResult.Content.ReadAsStringAsync();

            ////if (!postResult.IsSuccessStatusCode)
            ////{
            ////    throw new ApplicationException(postContent);
            ////}


            var postResult = await _client.PostAsync("/api/PlaceImageInfo", item);

            var postContent = await postResult.Content.ReadAsStringAsync();

            PlaceImageResponse response = JsonConvert.DeserializeObject <PlaceImageResponse>(postContent);

            if (!postResult.IsSuccessStatusCode)
            {
                throw new ApplicationException(postContent);
            }
            else
            {
                return(response);
            }
        }
Example #2
0
        public async Task <ActionResult <PlaceImageResponse> > PostPlaceImageInfo()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(_environment.WebRootPath))
                {
                    _environment.WebRootPath = Directory.GetCurrentDirectory();
                }

                if (Request.Form.Files.Count > 0)
                {
                    var file = Request.Form.Files[0];

                    var uploadFolder        = Path.Combine(_environment.WebRootPath, "Files"); //실제 사용 폴더
                    var uploadFolderProduct = Path.Combine(uploadFolder, "Temp");
                    if (!System.IO.Directory.Exists(uploadFolderProduct))
                    {
                        System.IO.Directory.CreateDirectory(uploadFolderProduct);
                    }

                    if (file.Length > 0)
                    {
                        //파일 이름 만들기 UserId + DateTime + extension
                        string extension = System.IO.Path.GetExtension(Path.GetFileName(ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"')));
                        //string strTimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        //strTimeStamp = strTimeStamp.Replace(":", "");
                        //strTimeStamp = strTimeStamp.Replace("-", "");
                        //strTimeStamp = strTimeStamp.Replace(" ", "");
                        //strTimeStamp = strTimeStamp.Insert(8, "-");
                        //strTimeStamp = strTimeStamp.Insert(13, "-");
                        var fileName = Guid.NewGuid() + extension;

                        //var dbPath = $"{Request.Scheme}://{Request.Host}/Temp/Product/{fileName}"; //호출하는 폴더

                        using (var fileStream = new FileStream(Path.Combine(uploadFolderProduct, fileName), FileMode.Create))
                        {
                            file.CopyTo(fileStream);
                        }

                        var placeImageInfo = new PlaceImageInfo();
                        placeImageInfo.FileName = fileName;
                        context.PlaceImageInfo.Add(placeImageInfo);

                        await context.SaveChangesAsync();

                        var response = new PlaceImageResponse()
                        {
                            Id       = placeImageInfo.Id,
                            FileName = fileName
                        };


                        return(Ok(response));
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, $"Internal server error: {ex}"));
            }
        }