zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    TreeSet类的排序问题
    TreeSet()详解
    css中vertical-align垂直居中的认识
    CSS3 float深入理解浮动资料整理
    层叠顺序与层叠上下文
    jquery.zclip轻量级复制失效问题
    文章转载利用border、transparent实现微风
    转载利用线性渐变实现晴天、多云特效
    转载利用伪元素单个颜色实现 hover 和 active 时的明暗变化效果
    MUI-最接近原生App体验的前端框架
  • 原文地址:https://www.cnblogs.com/nxhdw/p/7943285.html
Copyright © 2011-2022 走看看