当前位置:   article > 正文

Asp.Net Core文件上传IFormFile_asp.net core iformfile

asp.net core iformfile

文件上传功能在实际开发中经常使用,在 .Net Core中,文件上传接收类型不再使用 HttpPostedFile 或 HttpFileCollection来接收,而是使用 IFormFile 或 IFormFileCollection来接收。

  下面看一个例子就明白怎么使用了,具体代码如下:

  1. <div class="form-group">
  2. <form enctype="multipart/form-data" asp-controller="home" asp-action="upload" method="post">
  3. <input type="file" name="input" id="input" class="custom-file-input" />
  4. <label class="custom-file-label" for="input"></label>
  5. <input type="submit" value="提交" />
  6. </form>
  7. </div>
  8. @section scripts{
  9. <script>
  10. $(document).ready(function () {
  11. $(".custom-file-input").on("change", function () {
  12. var fileName = $(this).val().split("\\").pop();
  13. $(this).next(".custom-file-label").html(fileName);
  14. })
  15. });
  16. </script>
  17. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Mvc;
  7. using FileUpload.Models;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Hosting;
  10. using System.IO;
  11. namespace FileUpload.Controllers
  12. {
  13. public class HomeController : Controller
  14. {
  15. private readonly IHostingEnvironment _hostingEnvironment;
  16. public HomeController(IHostingEnvironment hostingEnvironment)
  17. {
  18. _hostingEnvironment = hostingEnvironment;
  19. }
  20. public IActionResult Index()
  21. {
  22. return View();
  23. }
  24. [HttpPost]
  25. public IActionResult Upload(IFormFile input)
  26. {
  27. if (input == null) return BadRequest();
  28. string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
  29. string uniqueFileName = Guid.NewGuid().ToString() + "_" + input.FileName;
  30. string filePath = Path.Combine(uploadsFolder,uniqueFileName);
  31. input.CopyTo(new FileStream(filePath, FileMode.Create));
  32. return Ok();
  33. }
  34. }
  35. }

 多文件上传

  1. <div class="form-group">
  2. <form enctype="multipart/form-data" asp-controller="home" asp-action="upload" method="post">
  3. <input type="file" name="input" id="input" class="custom-file-input" multiple />
  4. <label class="custom-file-label" for="input"></label>
  5. <input type="submit" value="提交" />
  6. </form>
  7. </div>
  8. @section scripts{
  9. <script>
  10. $(document).ready(function () {
  11. $(".custom-file-input").on("change", function () {
  12. var fileLabel = $(this).next(".coustom-file-lable");
  13. var files = $(this)[0].files;
  14. if (files.length > 1) {
  15. fileLabel.html("你已选择了" + files.length + "个文件");
  16. } else {
  17. fileLabel.html(files[0].name);
  18. }
  19. })
  20. });
  21. </script>
  22. }
  1. [HttpPost]
  2. public IActionResult Upload(IFormFileCollection input)
  3. {
  4. if (input == null) return BadRequest();
  5. string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
  6. string uniqueFileName = Guid.NewGuid().ToString() + "_" + input[0].FileName;
  7. string filePath = Path.Combine(uploadsFolder,uniqueFileName);
  8. input[0].CopyTo(new FileStream(filePath, FileMode.Create));
  9. return Ok();
  10. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/377864
推荐阅读
相关标签
  

闽ICP备14008679号