Easyui Issues uploading files

Source: Internet
Author: User

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

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.