JS XMLHttpRequest + FormData cross-domain submit form upload file

Source: Internet
Author: User

Funuploadfile:function (file) {var self = this;     In each of the this point is not a V so first keep this var formdata = new Formdata ();         Formdata.append ("index", File.index), Formdata.append ("FileList", file);  var xhr = new XMLHttpRequest (); if ("Withcredentials" in xhr) {//XHR for Chrome/firefox/opera/safari. Xhr.open ("POST", Self.url, True);}    else if (typeof xdomainrequest! = "undefined") {//Xdomainrequest for IE.    XHR = new Xdomainrequest (); Xhr.open ("POST", Self.url);} else {xhr = null;} Bind upload Event//Progress Xhr.upload.addEventListener (' Progress ', function (e) {if (e.lengthcomputable) {//callback to external self.onprogress (f  Ile, E.loaded, E.total); }}, False); Complete Xhr.addeventlistener ("Load", function (e) {//delete the uploaded file from file false is not executed OnDelete callback method Self.fundeletefile (File.index,  FALSE);  Callback to external self.onsuccess (file, xhr.responsetext);  if (Self.uploadFile.length = = 0) {//Callback all Complete method Self.oncomplete ("Complete All"); }}, False); Xhr.addeventlistener ("Error", function (e) {//callback to external self.onfailure (file, Xhr.responsetEXT);}, False);//xhr.setrequestheader ("Access-control-allow-origin", "*");//xhr.setrequestheader ("     X-requested-with "," XMLHttpRequest "); Xhr.setrequestheader ("Content-type", "Multipart/form-data"); Xhr.send (Formdata);//xhr.send ();}

  

The above-submitted HTML form resembles: <form id= "Uploadform" action= "http://fileserver.bbb.com/fileserver.ashx?action=upload&aid= 1 "method=" POST "enctype=" Multipart/form-data ">

Background configuration:

<system.webServer> 

Background processing with handler:

namespace webapp{//<summary>///handler///</summary> public class Handler:ihttphandl        Er {private HttpRequest _request;        Private HttpResponse _response;        Private HttpContext _context;            public void ProcessRequest (HttpContext context) {_context = context; _request = context.            Request; _response = context.            Response; var action = context.            Request.querystring["Action"];                    Switch (action) {case "upload": Upload ();                Break            Default:break; }} private void Upload () {if (_request. Files.count > 0) {} _response.        Write ("AA:" + "xxx");            } public bool IsReusable {get {return false; }        }    }}

  

A total of two requests were made during the cross-domain request, the first is an options request called preflight, which is sent to the server to let the server confirm that the cross-domain request was accepted, then returned to the browser for confirmation, and then made a second real request. As shown in the following:

JS XMLHttpRequest + FormData cross-domain submit form 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.