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>
  • 相关阅读:
    转ihone程序内发邮件,发短信,打开链接等
    plist 文件的读写
    转object c语法速成
    转iphone项目之间的引用。
    object c求nsstring 长度和去掉前后空格的方法
    object c runtime中类类型和消息支持检查
    转NSDictionary类使用
    设置UITableview 浮动的 header
    NSString 类型plist转为NSDictionary
    ObjectiveC Unicode 转换成中文
  • 原文地址:https://www.cnblogs.com/nxhdw/p/7943285.html
Copyright © 2011-2022 走看看