Index.cshtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@{ ViewData["Title"] = "Home Page"; Layout = null; } <div class="text-center"> <!-- 메시지를 표시하는 영역 --> <div> @ViewBag.Message </div> <br/> <form method="POST" enctype="multipart/form-data" action="/Home/UploadFile"> <!-- 파일 업로드 form enctype는 multipart/form-data는 메소드 타입은 POST, URL은 /Home/FileUpload이다. --> <input type="file" name="file" multiple /> <!-- file input 태그, 이름은 file, 복수 파일 첨부를 위해 multiple 속성을 추가한다. --> <input type="submit"> </form> </div> |
HomeeController.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using WebApplication3.Models; using System.IO; using Microsoft.AspNetCore.Http; namespace WebApplication3.Controllers { public class HomeController : Controller { private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { return View(); } public async Task<IActionResult> UploadFile(IFormFile file) { if (file == null || file.Length == 0) { return Content("No file selected for upload."); } var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/uploads", file.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } ViewBag.Message = "File Uploaded Successfully."; //return RedirectToAction("Index"); // Or any view you want to redirect to return View("Index"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } } |