asp.net core implements file upload function

高洛峰
Release: 2017-02-07 11:42:12
Original
2051 people have browsed it

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; }
}
Copy after login


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>
Copy after login


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));
}
Copy after login


Multiple file upload

Multiple file upload is similar to single file upload. The form's ViewModel uses ICollection, and then the form's 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!

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