First, add the following code to the front-end interface
<formID= "FM"Method= "POST"enctype= "Multipart/form-data"style= "margin:0;padding:20px 50px"> <Divstyle= "margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">Uploading files</Div> <Divstyle= "margin-bottom:10px"> <inputID= ' Upfile 'class= "Easyui-filebox"name= "File1"multiple= "true"data-options= "prompt: ' Select file ', ButtonText: ' Select File '"style= "width:300px"/> <aID= "Sub"href="#"class= "Easyui-linkbutton"Iconcls= "Icon-add"onclick= "Submit ()"Plain= "true">Uploading files</a> </Div></form>
Note: Easyui-filebox must be added with the name attribute, which cannot be obtained without the background.
Do not dwell on the use of $ (' #upfile '). Filebox (' GetValue ') get file path feedback for C:\fakepath\ ... problem, this is not related to uploading files really.
Write the corresponding JS code
functionSubmit () {$ (' #fm '). Form (' Submit ', {URL:'/uploaddemo/uploadfiles ', OnSubmit:function () { return$( This). Form (' Validate '); }, Success:function(Result) {varresult = eval (' (' + result + ') '); if(result.success) {$.messager.show ({title:Prompted, msg:' Upload success ' }); } Else{$.messager.show ({title:Error, MSG:RESULT.ERRORMSG}); } } }); }
The controller writes the upload file code
[HttpPost] PublicActionResult Uploadfiles () {varSuccess =false; varErrorMsg =""; //gets the collection of files uploaded by the clienthttpfilecollection files =System.Web.HttpContext.Current.Request.Files; if(Files. Count = =0) {errormsg="No files were obtained"; returnJson (New{success = success, errormsg =errormsg}); } Try { //define the destination path for file storage stringTargetDir = System.Web.HttpContext.Current.Server.MapPath ("~/content/upload/touxiang"); //get each file in a collection of files for(inti =0; I < files. Count; i++) {Httppostedfile file=Files[i]; //full path to the group composition file stringPath =System.IO.Path.Combine (TargetDir, System.IO.Path.GetFileName (file. FileName)); //Save the uploaded file to the specified pathfile. SaveAs (path); } Success=true; } Catch(System.Exception ex) {errormsg=Ex. ToString (); returnJson (New{success = success, errormsg =errormsg}); } returnJson (New{success = success, errormsg =errormsg}); }
Fix It ~
Easyui Issues uploading files