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