.net core 上傳文件Demo


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("上傳成功");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM