• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.NetCore实现文件上传

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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="width: 200px; height: 150px;">
       <img src="~/Images/AAAAAA&amp;text=no+image.png" />
               </div>
      <div class="fileupload-preview fileupload-exists img-thumbnail" style="width: 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>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net读取Xml文件并进行DropDownList数据绑定发布时间:2022-07-10
下一篇:
ASP.NETWebApi路由配置发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap