首页 > Web开发 > 详细

.net core 上传文件Demo

时间:2019-12-02 23:53:42      阅读:119      评论:0      收藏:0      [点我收藏+]

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

 

.net core 上传文件Demo

原文:https://www.cnblogs.com/zhouyg2017/p/11973357.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!