.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