zoukankan      html  css  js  c++  java
  • mvc5之文件上传

      在写这篇博客之前,被网上的博客坑了一个遍。

      浪费了很多的时间

      最后还是靠一位兄弟解决的问题,不得不说,虽然网上资源多。但是大多数都是水货。如果那些人可以解决你的问题,那我这里明显就没用了,除非你和我一样都是用的vs2017

      好了,直接上代码,这是前端的

     <!--upload 是控制器方法 后面那个是控制器名字-->
        <form action='@Url.Action("Upload", "FilesUpload")' method="post" enctype="multipart/form-data">
            <input type="file" name="file" />
            <input type="submit" value="提交" />
        </form>

    控制器的代码

    public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public ActionResult UploadX(HttpPostedFileBase file)
            {
                if (file != null)
                {
                    string fileName = Path.GetFileName(file.FileName);//这才是 vs2017的正确姿势
                    //string fileName = file.FileName.ToString(); 网上的大部分都是这种,我可以很负责的说 vs2017不可以!上传成功找不到文件,找不到!!
    
                    string path = Server.MapPath(string.Format("~/{0}", "File"));
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    file.SaveAs(Path.Combine(path, fileName));
                }
                return View();
    
            }
  • 相关阅读:
    有点成熟的短句,最新个性签名
    ACM2039_三角形三边关系
    Android 绘制中国地图
    Opengl-法线贴图(用来细化表面的表现表现的凹凸)
    Go的sync
    Laravel Study(使用 Laravel )
    对于宅男来说,硬盘里的数据就是命
    设计模式之模板方法模式
    游戏掉落道具掉落
    NEWMING
  • 原文地址:https://www.cnblogs.com/shichina/p/10179894.html
Copyright © 2011-2022 走看看