Asynchronously uploading a file using new FormData ($ (' #uploadForm ') [0]) to serialize the form

Source: Internet
Author: User
Tags datetime file upload serialization
$ ("form") for jquery. Serialize () and new Formdata form serialization

$ ("form"). Serialize () and new FormData ($ (' #uploadForm ') [0]) are serialized forms that implement asynchronous submissions of forms, but they differ

First, the former, can only serialize data in the form, such as text boxes, such as input select data, but for files, such as file upload, can not be achieved, then this time, Formdata on the stage,

New Formdata use requires a point of note,

Note One: The requirement for jquery is that it seems to be supported by version 1.8 and above.

In addition, the object can not only serialize files, but also can be used as the serialization of form data (that is, the function of serialize () is included);

Note Two: Look at the script.

 $.ajax ({type: ' POST ', Data:uploadformdata, url: '/artical/publist ',//typeerror: ' Append ' called on a object that does not implement interface FormData.processData:fals
                    E, Contenttype:false, Async:false, success:function (data) { if (typeof (data) = = undefined) {alert ("User information is missing, please login again.") ");
                    Window.parent (). Location.href = "/account/login"; } if (data. ErrorMsg = = "") {alert (' US release successful.
                    ‘); } else {alert (data. ERRORMSG);
  }
                }
            });


Note that the red part of the script as well as the description, Processdata:false, Contenttype:false, missing these two settings, will appear in the Red section of the error message, the submission failed. The following is a full front and rear reference script://Submit File Function Submitfile () {$ ('. Btn-publish '). Click (function () {//var title = $ ('. Txt-video-title '). Val (); 
	var uploadformdata = new FormData ($ (' #uploadForm ') [0]);	
Serialize the form, $ ("form"). Serialize () can only serialize data, cannot serialize file       
     $.ajax ({
                type: ' POST ',
                data:uploadformdata,
                URL: '/ Artical/publist ',//typeerror: ' Append ' called on a object that does not implement interface FormData.
                Processdata:false,
                Contenttype:false,
                async:false,
                success:function (data) {
                    if (typeof (data) = = Undefined) {
                        alert ("User information is missing, please sign in again. "); Window.parent (). Location.href = "/account/login";
                    }
                    if (data. ErrorMsg = = "") {
                        alert (' US release successful. ');
                    } else {alert (data). ERRORMSG); }
                }
            });

        });
    }


<summary>///Upload new picture, (including file upload)///</summary>///<returns></returns>public Jsonresult UpLoad () {if (null! = Session[consts.systemuersession]) {string picture Name = request["Videotitle"];//picture title string pictureinfourl = "";//virtual path after image upload string picturecategorykey = request[" Picturecategorylist "];//Video classification foreign key id Fileuploadresult fileuploadpicture = null;//for output string filesavepath = C Onsts. Picturesavepath + DateTime.Now.ToString ("yyyyMMdd") + "/";//The time of the day most folder string fileName = DateTime.Now.ToString (" Yyyymmddhhmmssfff ");//The file name generated//upload, if it is a video file, automatically generated by transduction fileuploadpicture = Request.upload (fi

                Lesavepath, NULL, "", FileName, ""); #region Boxing, Inbound if (fileuploadpicture.filesavepath! = null) {foreach (V AR path in Fileuploadpicture.filesavepath) {Pictureinfourl + = (Path + ",");
                    } Pictureinfourl = Pictureinfourl.remove (pictureinfourl.length-1, 0);
                        Columnpicture picture = new Columnpicture () {Id = Combhelper.newcomb (),
                        Picturetitle = picturename, picturetitledescription = Pictureinfourl, Goodclicktimes = 888, Badclicktimes = ten, adddate = Datetim E.now, Favoritetimes = 888, IsEnabled = true, ToTo p = 0, Customerkey = ((Customer) session[consts.systemuersession]).
                    Id, Columnscategorykey = new Guid (Picturecategorykey)}; if (_pictureservice.insert (picture)) {fileuploadpicture = new Fileuploadresul
    T () {                        Status = true, Filesavepath = null, Errorm
                    sg = ""};
            }} #endregionreturn Json (Fileuploadpicture, jsonrequestbehavior.allowget);
        } return null; }

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.