Home  >  Article  >  Backend Development  >  asp.net core implements file upload function

asp.net core implements file upload function

高洛峰
高洛峰Original
2017-02-07 11:42:122088browse

The examples in this article share the functions of single file upload and multiple file upload for your reference. The specific content is as follows

Single file upload
Uploading files is a common function in Web applications. . It is very easy to upload files in asp.net core and save them on the server. Let's demonstrate how to upload files in an ASP.NET Core project.
First, create an asp.net core project, then add a HomeController to the Controller file, and then add a New.cshtml view file to the Home folder of the Views folder. As shown below:

asp.net core实现文件上传功能

Add a UserViewModel.cs in the Model folder, the code is as follows:

public class UserViewModel
{
  [Required]
  [Display(Name = "姓名")]
  public string Name { get; set; }
 
  [Required]
  [Display(Name = "身份证")]
  [RegularExpression(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", ErrorMessage = "身份证号不合法")]
  public string IdNum { get; set; }
 
  public string IdCardImgName { get; set; }
 
  [Required]
  [Display(Name = "身份证附件")]
  [FileExtensions(Extensions = ".jpg,.png", ErrorMessage = "图片格式错误")]
  public IFormFile IdCardImg { get; set; }
}


Then add A New.cshtml view file is in the Views folder:

@model UserViewModel
 
<form asp-controller="Home" role="form" asp-action="New" enctype="multipart/form-data" method="post">
  <div class="form-group">
    <label asp-for="Name"></label>
    <input type="text" class="form-control" asp-for="Name" />
  </div>
  <div class="form-group">
    <label asp-for="IdNum"></label>
    <input type="text" class="form-control" asp-for="IdNum" />
  </div>
  <div class="form-group">
    <label asp-for="IdCardImg"></label>
    <input type="file" asp-for="IdCardImg" />
    <p class="help-block">上传。</p>
  </div>
  <button type="submit" class="btn btn-default">提交</button>
</form>


In HomeController, add the Action method corresponding to the page:

[HttpPost]
public IActionResult New([FromServices]IHostingEnvironment env, [FromServices]AppDbContext dbContext, UserViewModel user) {
  var fileName = Path.Combine("upload", DateTime.Now.ToString("MMddHHmmss") + ".jpg");
  using (var stream = new FileStream(Path.Combine(env.WebRootPath, fileName), FileMode.CreateNew)) {
    user.IdCardImg.CopyTo(stream);
  }
 
  var users = dbContext.Set<User>();
  var dbUser = new User() {
    Name = user.Name,
    IdCardNum = user.IdNum,
    IdCardImgName = fileName
  };
  users.Add(dbUser);
  dbContext.SaveChanges();
 
  return RedirectToAction(nameof(Index));
}


Multiple file upload

Multiple file upload is similar to single file upload. The form's ViewModel uses ICollection6e1c43e97cfd097c17aa357221607968, and then the form's 4d309b2565bbabcde67112e152382251 Just add mulpitle (only supports H5).

Sample source code
Note: The sample data storage uses Sqlite, and the Code First method is used to generate the database.

For more articles related to asp.net core's implementation of file upload function, please pay attention to 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