File upload, IFRAME implementation of cross-domain data submission _javascript skills

Source: Internet
Author: User

1, file upload, picture upload, Third-party uploadify plug-ins, http://www.uploadify.com/about/

2, the use of IFrame upload files, submit the form, the main idea is:

A, JS create form form, IFRAME, add to the body, form target and the name of the IFrame consistent.

B, Form form to update the data, submit submission

C, if uploaded files, pictures, form inside add input-file elements, Binding onchange event, JS Trigger, add submit event inside OnChange

D, on the callback: After the success of the IFRAME submission, the returned data in the IFRAME inside the page, you can get this, you need to split:

$iframe [0].contentwindow.document.queryselector (' body '). InnerHTML

Code slices:

var target = ' framefile '; var input = ' <input type= "file" name= "file" onchange= "Changeimage ()" > ' + ' <input name= "appId" value= "' +id+ '"
; ' + ' <input type= "submit" name= "submit" value= "submit" > "; var $form = $ (' <form action= ' + URL + ' "target=" ' + Target + ' "enctype=" Multipart/form-data "method=" post "id=" Upimagefo
RM "style=" Display:none "> ' +input+ ' </form>"). Appendto (' body ');
var $iframe = $ (' <iframe name= ' + target + ' "style=" Display:none "></iframe>"). Appendto (' body ');
$form. Find (' input ')-eq (0). Click ();

Window.changeimage = function () {$form. Find (' input '). EQ ( -1). click ();}; Detects the IFRAME page load $iframe. Load (function (e) {var str = $iframe [0].contentwindow.document.queryselector (' body ').
  InnerHTML;
  var data = SPT (str);
  $form. Remove ();
  $iframe. Remove ();
callback (data);
});
  function SPT (str) {//string is split into object var a = Str.indexof (' {');
  var B = str.indexof ('} ');
  var j = {};
  var t = [];
  var s = str.substring (a+1,b); s = s.replace (/"/gi,'');
  t = S.split (', ');
    for (var i=0 i<t.length; i++) {var tmp = T[i].split (': ');
  J[tmp[0]] = tmp[1];
  };
Return J; };

2-c correction, the above code has bugs:

Manually triggering the Input-file onchange event and submit it in the event is problematic. Because the onchange event is triggered, the selection begins and the submit is submitted.

Upload function, form form elements can only be placed in HTML.

The above file upload, iframe cross-domain data submission is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.