asp.net完成文件上传的代码教程

Y2J
Release: 2017-05-16 10:25:36
Original
1386 people have browsed it

本篇文章主要介绍了asp.net core mvc实现文件上传实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

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

Controller:        

public class PictureController : Controller
  {
    private IHostingEnvironment hostingEnv;

    public PictureController(IHostingEnvironment env)
    {
      this.hostingEnv = env;
    }
    // GET: //
    public IActionResult Index()
    {
      return View();
    }
    public IActionResult UploadFiles()
    {
      return View();
    }
    [HttpPost]
    public IActionResult UploadFiles(IList 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();
    }

  }
Copy after login

view:

Copy after login

文件是上传到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:\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);
    }
Copy after login

view

Copy after login

jquery

Copy after login

【相关推荐】

1. 特别推荐“php程序员工具箱”V0.1版本推荐

2. ASP免费视频教程

3. 李炎恢ASP基础视频教程

The above is the detailed content of asp.net完成文件上传的代码教程. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!