view:
<form method="post" enctype="multipart/form-data" action="@Url.Action("Upload")">
<input type="file" id="file" name="file"/>
<button>提交</button>
</form>
controlller:
public IActionResult Upload() { var file = Request.Form.Files[0]; if (file==null||file.Length==0) { return Content("檔案為空"); } var pathBase = Directory.GetCurrentDirectory(); var uploadPath = Path.Combine(pathBase, "Upload"); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string name = Guid.NewGuid().ToString() + file.FileName; string filePath = Path.Combine(uploadPath,name); using (var statem=System.IO.File.Create(filePath)) { file.CopyTo(statem); } return Content("上傳成功"); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/87864.html
標籤:.NET Core
上一篇:ASP.NET Core Web 應用程式系列(五)- 在ASP.NET Core中使用AutoMapper進行物體映射
