Home  >  Article  >  Backend Development  >  Code tutorial for completing file upload in asp.net

Code tutorial for completing file upload in asp.net

Y2J
Y2JOriginal
2017-05-16 10:25:361394browse

This article mainly introduces the example of asp.net core mvc to implement file upload, which has certain reference value. Interested friends can refer to it.

The file upload function is used in my work. In this sharing~~

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();
    }

  }

view:

The file is uploaded to the wwwroot directory It’s under the file. I can’t quite understand it and I’m still learning. Everyone is welcome to communicate~~

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

The following is jquery ajax upload The z parameter of the

post action is useless because there is only one post action that will cause a 404 error, so I added another 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);
    }

view

jquery

【Related recommendations】

1. Special recommendation:"php programmer toolbox "V0.1 version recommended

2. ASP free video tutorial

3. Li Yanhui ASP basic video tutorial

The above is the detailed content of Code tutorial for completing file upload in asp.net. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 admin@php.cn