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