asp.net core mvc实现文件上传实例

高洛峰
发布: 2017-02-07 11:33:03
原创
1167 人浏览过

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

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(&#39;"&#39;);
        //这个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(&#39;"&#39;);
    filename = @"C:\Users\lg.HL\Desktop" + $@"\{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>
登录后复制

   


欢迎大家交流~ 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

更多asp.net core mvc实现文件上传实例相关文章请关注PHP中文网!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板