Asp.net core implements file upload and core File Upload

Source: Internet
Author: User

Asp.net core implements file upload and core File Upload

Examples of this article are as follows:Single file upload and multi-File UploadFor your reference, the specific content is as follows:

Upload a single file
Uploading files is a common feature in Web applications. It is easy to upload files in asp.net core and save them on the server. The following shows how to upload files in the ASP. NET Core project.
First, create an asp.net core project, add a HomeController in the Controller file, and add a New. cshtml View File in the Home folder of the Views folder. For example:

Add UserViewModel. cs to the Model folder. The Code is as follows:

Public class UserViewModel {[Required] [Display (Name = "Name")] public string Name {get; set;} [Required] [Display (Name = "ID card")] [RegularExpression (@ "^ (\ d {15 }$ | ^ \ d {18 }$ | ^ \ d {17} (\ d | X | x) $ ", errorMessage = "invalid ID card number")] public string IdNum {get; set;} public string IdCardImgName {get; set ;} [Required] [Display (Name = "ID card attachment")] [FileExtensions (Extensions = ".jpg,.png", ErrorMessage = "image format error")] public IFormFile IdCardImg {get; set ;}}

Then add a New. cshtml View File 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- = "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"> upload. </P> </div> <button type = "submit" class = "btn-default"> submit </button> </form>

In HomeController, add the corresponding Action method of 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));} 

Run the program and view the form:


Multi-File Upload

Multifile upload is similar to single file upload. The ViewModel of a form uses ICollection <IFromFile>, then, add the form <input type = "file" asp-for = "IdCardImg" mulpitle/> to mulpitle (only H5 is supported ).

Sample source code
Note:The sample data storage uses Sqlite and Code First to generate a database.
Sample Code has been uploaded to github: https://github.com/yuleyule66/AspNetCoreFileUpload

Address: http://www.cnblogs.com/savorboard/p/5599563.html
Author's blog: Savorboard

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.