• Asp.Net Core实现文件上传


    1. Asp.Net Core Mvc方式

    public class UploadController : Controller
        {
            private IHostingEnvironment _hostingEnv;
            public UploadController(IHostingEnvironment hostingEnv)
            {
                _hostingEnv = hostingEnv;
            }
            [HttpPost]
            public IActionResult Index()
            {
                var file = Request.Form.Files;           
                if (file.Sum(f => f.Length) > 0)
                {
                    foreach (var pic in file)
                    {
                        var picname = ContentDispositionHeaderValue
                                    .Parse(pic.ContentDisposition)
                                    .FileName
                                    .Trim('"');
                        var exname = picname.Substring(picname.LastIndexOf("."));
                        var picfullname = DateTime.Now.ToString() + exname;
                        picname = _hostingEnv.WebRootPath + $@"UploadImg{picfullname}";
    
                        using (FileStream fs = System.IO.File.Create(picname))
                        {
                            pic.CopyTo(fs);
                            fs.Flush();
                        }
                    }
                }
               
                return Content("ok");
            }
        }

    2.Asp.Net Mvc方式

     public class UploadController : Controller
        {   
            [HttpPost]
            public ActionResult Index(HttpPostedFileBase picture)
            {
                if (picture.ContentLength > 0)
                {             
                    string filePath = Path.Combine(HttpContext.Server.MapPath("../UploadImg"),
                       Path.GetFileName(picture.FileName));
                    picture.SaveAs(filePath);                                                 
                }
                return Content("ok");
            }
        }

    HTML页面

    <form method="post" enctype = "multipart/form-data">
     <div class="form-group">
    <label class="control-label col-md-2">预览图</label>
    <div class="col-md-4">
      <div class="fileupload fileupload-new" data-provides="fileupload">
        <div class="fileupload-new img-thumbnail" style=" 200px; height: 150px;">
           <img src="~/Images/AAAAAA&amp;text=no+image.png" />
                   </div>
          <div class="fileupload-preview fileupload-exists img-thumbnail" style=" 200px; max-height: 150px"></div>
           <div>
         <span class="btn btn-default btn-file"><span class="fileupload-new">选择图片</span><span class="fileupload-exists">更换</span><input type="file" name="pic" id="picture"></span><a class="btn btn-default fileupload-exists" data-dismiss="fileupload" href="#">清除</a>
          </div>
        </div>
          </div>
     </div>
    </form>
  • 相关阅读:
    mybatis动态sql中的两个内置参数(_parameter和_databaseId)
    Vue.js项目部署在Tomcat服务器上
    Vue2.0 + ElementUI的+ PageHelper实现的表格分页
    mybatis中使用mysql的模糊查询字符串拼接(like)
    C++循环链表解决约瑟夫环问题
    Nginx源码分析-ngx_module_s结构体
    设计模式(一)工厂模式Factory(创建型)(转)
    网络编程--套接字选项(一)
    Linux阵列 RAID详解 (转)
    HDFS RAID实现方案(转)
  • 原文地址:https://www.cnblogs.com/nxhdw/p/7943285.html
Copyright © 2020-2023  润新知