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 core and save them on the server. Let's show you how to upload files in the core project.
First, create a 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
  [Display (name = ' name ']] public
  string Name {get; set;}

  [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;}

  [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 class= "Form-group" >
    <label asp-for= "Idnum" ></label>
    <input type= "text" class= "Form-control" asp-for= "Idnum"/ >
  <div class= "Form-group" >
    <label asp-for= "Idcardimg" ></label>
    <input type= "file" asp-for= "idcardimg"/> <p class=
    "Help-block" > uploaded. </p>
  <button type= "Submit" class= "BTN Btn-default" > Submit </button>
< /form> 

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

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:

This article address:
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: 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.