첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

파일 업로드를 처리하는 컨트롤러의 액션 메소드에 RequestFormLimits/RequestSizeLimit 어트리뷰트를 아래와 같이 추가한다.

 

▶ Controllers/TestControllers.cs

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace TestProject.Controllers
{
    /// <summary>
    /// 테스트 컨트롤러
    /// </summary>
    public class TestController : Controller
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 업로드 페이지 처리하기 - Upload()

        /// <summary>
        /// 업로드 페이지 처리하기
        /// </summary>
        /// <returns>액션 결과</returns>
        [HttpGet]
        public IActionResult Upload()
        {
            return View();
        }

        #endregion
        #region 업로드 페이지 처리하기 - Upload(environment, formFileCollection)

        /// <summary>
        /// 업로드 페이지 처리하기
        /// </summary>
        /// <param name="environment">웹 호스트 환경</param>
        /// <param name="formFile">폼 파일</param>
        /// <returns>액션 결과</returns>
        [HttpPost]
        [RequestFormLimits(MultipartBodyLengthLimit = 209715200)]
        [RequestSizeLimit(209715200)]
        public IActionResult Upload([FromServices]IWebHostEnvironment environment, ICollection<IFormFile> formFileCollection)
        {
            string uploadDirectoryPath = Path.Combine(environment.WebRootPath, "upload");

            long totalSize = 0L;

            foreach(IFormFile formFile in formFileCollection)
            {
                string uploadFilePath = Path.Combine(uploadDirectoryPath, formFile.FileName);

                using(FileStream fileStream = System.IO.File.Create(uploadFilePath))
                {
                    formFile.CopyTo(fileStream);

                    fileStream.Flush();
                }

                totalSize += formFile.Length;
            }

            if(formFileCollection.Count == 1)
            {
                IFormFile formFile = formFileCollection.First();

                ViewData["message"] = $"{formFile.FileName} 파일이 업로드되었습니다 : {formFile.Length:#,##0} 바이트";
            }
            else
            {
                ViewData["message"] = $"{formFileCollection.Count:#,##0}개 파일이 업로드되었습니다 : {totalSize:#,##0} 바이트";
            }

            return View();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요