MVC Ajax Upload File

Source: Internet
Author: User

Nonsense not much to say, on the code:

JS file used:

Jquery.min.js

Jquery.easyui.min.js

<inputID= "FileURL"onclick= "$ (' #imageUpLoad ')." click ();/><inputtype= "button"value= "Upload"onclick= "document.getElementById" (' Form1 '). Submit (); ">   <Divstyle= "display:none; position:absolute; top:0; left:0;"><iframename= "Uploadresponse"></iframe>    <formID= "Form1"runat= "Server"Action= "/uploadfile/uploadimage/"Method= "POST"enctype= "Multipart/form-data"Target= "Uploadresponse">         <inputtype= "File"ID= "Imageupload"name= "Imageupload"style= "Display:none;"onchange= "$ (' #fileurl '). attr (' isload ', ' 0 ');">         <inputID= "ImageType"name= "ImageType"value= "Floor"style= "Display:none;"/>    </form>    <Scripttype= "Text/javascript">        functionCallBack (Path) {$ ("#fileurl"). attr ('Isload', '1'). val (path); $.messager.alert ('Prompt Information', 'Upload Success! ', 'Info'); }         </Script></Div>

The Isload property is a property that I add myself to determine whether the picture is submitted

PS: Remember to initialize this property in the Window.load event, because the low version of IE does not support <input isload= "1"/> This format

Background code

usingsystem.web;usingSYSTEM.WEB.MVC; Public classUploadfilecontroller:controller {//        //GET:/uploadfile/         PublicActionResult Index () {returnView (); }         Public voidUploadimage (HttpPostedFileBase imageupload,stringImageType) {            stringReturnfilepath =Imageupload.filename; //This is my own piece of writing. Create a file path and save the code, you can directly imageupload.saveas ("File path"); to saveHelper.savefile (Imageupload, Helper.FileType.Images, ImageType,refReturnfilepath); Returnfilepath = Returnfilepath.replace ("\\","/"); Response.Write (string. Format ("<script type= ' text/javascript ' >window.parent.callback ('"+ Returnfilepath +"');</script>")); }    }
using System; using System.Collections.Generic; using System.Linq; using system.web; using System.IO;  Public class helper{

<summary>
File type
</summary>
public enum FileType {
Images,
Files
}

     Public Static BOOLSaveFile (HttpPostedFileBase F, FileType ft,stringSeparatorref stringpath) {            Try            {                stringFilePath =path.combine (AppDomain.CurrentDomain.BaseDirectory, Ft.)                ToString ()); if(!directory.exists (FilePath))                Directory.CreateDirectory (FilePath); FilePath=Path.Combine (FilePath, separator); if(!directory.exists (FilePath))                Directory.CreateDirectory (FilePath); FilePath=Path.Combine (FilePath, Path);                F.saveas (FilePath); Path=path.combine (ft.                ToString (), separator, path); return true; }            Catch(Exception ex) {loghelper.error (ex); }            return false; }}

Here is the main explanation of the HTML interface form parameters:

Action: Your MVC handles file upload paths

Method: How to submit

Enctype: This must be written to "Multipart/form-data"

Target: The value "Uploadresponse" indicates the receiving page address of the callback, here is an IFRAME, avoids the callback refreshes the page

Background CS Code Description:

Uploadimage: Method names are consistent with address
Parameters:
HttpPostedFileBase Imageupload
Imageupload should be the name value of the upload file control
String ImageType

Ibid. iamgetype as the name value of the text control
If you have multiple parameters that need to be passed to the background, you can customize the number of parameters in this format

Of course there is a way to put functions without parameters
Use request to process the files and parameters that you have passed, please check the information on your own.

MVC Ajax Upload File

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.