• ,net core mvc 文件上传


     工作用到文件上传的功能,在这个分享下 ~~

    Controller:         

           

    public class PictureController : Controller
        {
            private IHostingEnvironment hostingEnv;
    
            public PictureController(IHostingEnvironment env)
            {
                this.hostingEnv = env;
            }
            // GET: /<controller>/
            public IActionResult Index()
            {
                return View();
            }
            public IActionResult UploadFiles()
            {
                return View();
            }
            [HttpPost]
            public IActionResult UploadFiles(IList<IFormFile> files)
            {
                long size = 0;
                foreach (var file in files)
                {
                    var filename = ContentDispositionHeaderValue
                                    .Parse(file.ContentDisposition)
                                    .FileName
                                    .Trim('"');
    //这个hostingEnv.WebRootPath就是要存的地址可以改下 filename
    = hostingEnv.WebRootPath + $@"{filename}"; size += file.Length; using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } ViewBag.Message = $"{files.Count} file(s) /{ size}bytes uploaded successfully!"; return View(); } }

    view:

       

    <form asp-action="UploadFiles"
                      asp-controller="Picture"
                      method="post"
                      enctype="multipart/form-data">
                    <input type="file" name="files" multiple />
                    <input type="submit" value="Upload Selected Files" />
     </form>

    文件是上传到wwwroot目录文件下的,这我也看不太懂还在学习,欢迎大家交流~~

    ----------------------------------------------------------------------------------------------------------

    下面是jquery ajax方式上传的

    post方式的action的z参数没用 因为只有一个post方式的会404错误所以又加了一个get的action

    Controller:  

            public IActionResult UploadFilesAjax()
            {
                return View();
            }
            [HttpPost]
            public IActionResult UploadFilesAjax(string z)  
            {
                long size = 0;
                var files = Request.Form.Files;
                foreach (var file in files)
                {
                    var filename = ContentDispositionHeaderValue
                                    .Parse(file.ContentDisposition)
                                    .FileName
                                    .Trim('"');
                    filename = @"C:Userslg.HLDesktop" + $@"{filename}";        
                    size += file.Length;
                    using (FileStream fs = System.IO.File.Create(filename))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }
                }
                string message = $"{files.Count} file(s) / { size}bytes uploaded successfully!";
                    return Json(message);
            }

    view

    <form method="post" enctype="multipart/form-data">
                <input type="file" id="files"
                       name="files" multiple />
                <input type="button"
                       id="upload"
                       value="Upload Selected Files" />
     </form>

    jquery

    <script type="text/javascript">
            $(document).ready(function () {
                $("#upload").click(function (evt) {
                    var fileUpload = $("#files").get(0);
                    var files = fileUpload.files;
                    var data = new FormData();
                    for (var i = 0; i < files.length ; i++) {
                        data.append(files[i].name, files[i]);
                    }
                    $.ajax({
                        type: "POST",
                        url: "/Picture/UploadFilesAjax",
                        contentType: false,
                        processData: false,
                        data: data,
                        success: function (message) {
                            alert(message);
                        },
                        error: function () {
                            alert("There was error uploading files!");
                        }
                    });
                });
            });
    </script>

    欢迎大家交流~ 

    文章参考网址http://www.binaryintellect.net/articles/f1cee257-378a-42c1-9f2f-075a3aed1d98.aspx    (要翻~墙)

  • 相关阅读:
    WUSTOJ 1247: 递增或递减排序(Java)
    WUSTOJ 1246: 字符串排序(Java)
    Oracle查询部门工资最高员工的两种方法 1、MAX()函数 2、RANK()函数
    Oracle数据库——用户(USER)
    Firefox在新标签页打开“书签”和“搜索栏”(无需插件)
    Oracle数据库——ROWNUM
    Oracle数据库 SET ECHO [ON|OFF]
    WUST Oracle数据库 实验一实验二
    优课在线 嵌入式系统(胡威)2018春季测验 参考解析
    Oracle数据库——查询所有用户
  • 原文地址:https://www.cnblogs.com/intro/p/6235396.html
Copyright © 2020-2023  润新知