Implementation example of a pseudo ajax Image Upload code

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
If ($ _ FILES ){
?>
<Script>
Window. parent. ajaxUploadPicture. uploadCallback ('HTTP: // callback ');
</Script>
<? Php
Die;
}
?>
<! DOCTYPE html>
<Html>
<Head>
<Script>


AjaxUploadPicture = {$ o1 :''};


AjaxUploadPicture. ajaxUploadFile = function ($ o ){
This. $ o1 = $ o;
Var $ iframe = document. createElement ('iframe ');
$ Iframe. style. display = 'none ';
$ Iframe. name = 'iframe ';
$ Iframe. id = 'iframe ';
Document. body. appendChild ($ iframe );


Var $ form = document. createElement ("form ");
$ Form. method = "post ";
$ Form. enctype = "multipart/form-data ";
$ Form. action = "";
Required form.tar get = 'iframe ';
$ NewO = $ o. cloneNode ();
$ Form. appendChild ($ newO );
$ Form. submit ();
}
AjaxUploadPicture. uploadCallback = function ($ url ){

$ Pic = document. createElement ('img ');
$ Pic. width = "200 ";
$ Pic. height = "150 ";
$ Pic. src = $ url;

If (document. getElementById ('picshow') = null ){
$ Div = document. createElement ("div ");
$ Div. id = 'picshow ';
This. $ o1.parentNode. insertBefore ($ div, this. $ o1.nextSibling );
} Else {
$ Div = document. getElementById ('picshow ');
}
$ Div. innerHTML = '';
$ Div. appendChild ($ pic );
};
</Script>
</Head>
<Body>
<Input type = "file" name = "file" onchange = "ajaxUploadPicture. ajaxUploadFile (this);">
</Body>
</Html>

The object-oriented idea is that form is submitted to a hidden iframe and the callback js function is executed. Enable image echo. The backend php functions are not complete.

Related Article

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.