asp.net core Implementation File Upload function _ Practical skills

Source: Internet
Author: User
Tags button type

The example of this article for everyone to share the single file upload, multiple file upload function for your reference, the specific content as follows

Single File Upload
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. Let's show you how to upload files in the ASP.net core project.
First, create a asp.net core project, add a HomeController to the controller file, and add a new.cshtml view file in the Views folder's home folder. The following figure:

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

 public class Userviewmodel
{
  [Required]
  [Display (name = ' name ']] public
  string Name {get; set;}

  [Required]
  [Display (Name = "identity 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 = "Picture 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-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" > uploaded. </p>
  </div>
  <button type= "Submit" class= "BTN Btn-default" > Submit </button>
< /form> 

In HomeController, add the corresponding Action method for 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 to view the form:


Multiple file uploads

Multi-file upload and single file upload similar to the form of ViewModel using icollection<ifromfile>, and then the form of <input type= "file" asp-for= "idcardimg" mulpitle/ > Add mulpitle on it (only support H5).

Sample source Code
Note: The sample data store uses the Sqlite, Code-I method to generate the database.
Sample code has been uploaded to Github:https://github.com/yuleyule66/aspnetcorefileupload

This article address: http://www.cnblogs.com/savorboard/p/5599563.html
Author Blog: Savorboard

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.