ASP. NET complete File Upload Code tutorial

Source: Internet
Author: User
This article mainly introduces the ASP. NET core MVC implementation File Upload example, with a certain reference value, interested in small partners can refer to.

Work with the file upload function, under this share ~ ~

Controller:

public class Picturecontroller:controller {private Ihostingenvironment hostingenv;    Public Picturecontroller (Ihostingenvironment env) {this.hostingenv = env;    }//GET:/<controller>/public iactionresult Index () {return View ();    } public Iactionresult Uploadfiles () {return View ();      } [HttpPost] public iactionresult uploadfiles (ilist<iformfile> files) {long size = 0; foreach (var file in files) {var filename = contentdispositionheadervalue. Parse (file. contentdisposition). FileName.        Trim (' "');        This hostingenv.webrootpath is to save the address can be changed under filename = Hostingenv.webrootpath + $@ "\{filename}"; Size + = file.        Length; using (FileStream fs = System.IO.File.Create (filename)) {File.          CopyTo (FS); Fs.        Flush (); }} viewbag.message = $ "{files.       Count} file (s)/{size}bytes uploaded successfully! "; REturn View (); }  }

View

<form asp-action= "Uploadfiles"         asp-controller= "picture"         method= "post" enctype=         "multipart/ Form-data ">        <input type=" file "name=" files "multiple/>        <input type=" Submit "value=" Upload Selected Files "/> </form>

The file is uploaded to the wwwroot directory file, which I also do not understand is still learning, welcome everyone to Exchange ~ ~

----------------------------------------------------------------------------------------------------------

Here's the jquery Ajax way to upload

The z-parameter of the action of the post mode is useless because there is only one post method that will be 404 error, so add a Get action

Controller:

    Public Iactionresult Uploadfilesajax ()    {      return View ();    }    [HttpPost]    Public Iactionresult Uploadfilesajax (string z)     {      long size = 0;      var files = Request.Form.Files;      foreach (var file in files)      {        var filename = contentdispositionheadervalue                . Parse (file. contentdisposition)                . FileName                . Trim (' "');        filename = @ "C:\USERS\LG. Hl\desktop "+ $@" \{filename} ";            Size + = file. Length;        using (FileStream fs = System.IO.File.Create (filename))        {          File. CopyTo (FS);          Fs. Flush ();        }      }      String message = $ "{files. Count} file (s)/{size}bytes uploaded successfully! ";        return Json (message);    }

View

<form method= "POST" enctype= "Multipart/form-data" >      <input type= "file" id= "Files"          name= "Files" Multiple/>      <input type= "button"          id= "upload"          value= "upload Selected Files"/> </form>

Jquery

<script type= "Text/javascript" >    $ (document). Ready (function () {      $ ("#upload"). Click (function (evt) {        var fileUpload = $ ("#files"). Get (0);        var files = fileupload.files;        var data = new FormData ();        for (var i = 0; i < files.length; i++) {          data.append (files[i].name, files[i]);        }        $.ajax ({          type: "POST",          URL: "/picture/uploadfilesajax",          Contenttype:false,          Processdata:false,          Data:data,          success:function (message) {            alert (message);          },          error:function () {            Alert ("There was error uploading files!");});});    </script>

"Recommended"

1. Special recommendation : "PHP programmer Toolbox" V0.1 version recommended

2. asp free Video Tutorial

3. Eon the ASP Basic video tutorial

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.