JS dynamic create upload form through an IFRAME simulation Ajax to achieve no refreshing _javascript skills

Source: Internet
Author: User
Copy Code code as follows:

<script>
Window.onload=function () {
Upfile (' file.php ');
}


/*
* * URL Path
**/
function Upfile (URL) {
Create an IFRAME
var iframe = document.createelement ("iframe");
Document.body.appendChild (IFRAME);
iframe.id = ' iframename ';
Iframe.name = ' iframename ';
Iframe.style.display = ' None ';
Create a form
var turnform = document.createelement ("form");
Document.body.appendChild (Turnform);
Turnform.method = ' post ';
turnform.action = URL;
turnform.encoding = "Multipart/form-data";
Turnform.name = ' Formnamer ';
Turnform.target = ' iframename ';
Create a hidden form
var newelement = document.createelement ("input");
Newelement.setattribute ("name", "InputName");
Newelement.setattribute ("type", "file");
Newelement.setattribute ("Value", "");
Turnform.appendchild (newelement);
}

Form Submit
function Formsubmit () {
var res = Document.formNamer.inputname.value;
Document.formNamer.submit ();
}

return value
function callback (Result) {
Window.parent.document.getElementById (' Iframename '). style.display = ' block '; View background data through an IFRAME
if (result[' error '] = = 0) {
document.getElementById (' Test '). src= result[' img '][1]+ '/' +result[' img '][2];
}else if (result[' error '] = = 1) {
Alert (result[' Meg ']);
}else if (result[' error '] = = 2) {
Alert (result[' Meg ']);
}else if (result[' error '] = = 3) {
Alert (result[' Meg ']);
}else if (result[' error '] = = 4) {
Alert (result[' Meg ']);
}else{
Alert (result[' Meg ']);
}

}

</script>

<a href= "Javascript:formsubmit ()" > Upload </a>


file.php
Copy Code code as follows:

<?php
Header ("content-type:text/html; Charset=utf-8 ");

if (@is_uploaded_file ($_files[' inputname '] [' tmp_name '])) {

$f = $_files[' InputName '];
$name = $f ["name"];
$size = $f ["Size"];
$type = $f ["type"];
$fileName = $f ["Tmp_name"];

Switch ($type) {
Case ' image/jpg ': $okType = true;
Break

Case ' image/jpeg ': $okType = true;
Break

Case ' image/png ': $okType = true;
Break

Case ' image/gif ': $okType = true;
Break
}

if ($okType) {
$error = $f ["Error"];
Echo ' File name: '. $name. ' <br> ';
echo ' File type: '. $type. ' <br> ';
echo ' File size: '. Round ($size/1024). ' K<br> ';
Echo ' File temporarily holds path: '. $fileName. ' <br> ';

$fileDir = DirName (__file__). ' /img/upfile '. Time (). $name;
$img = explode ('/', $fileDir);

Move_uploaded_file ($fileName, $fileDir);

$data = Array ();
$data [' img '] = $img;
$data [' name '] = $name;
$data [' type '] = $type;
$data [' size '] = $size;
$data [' filename '] = $fileName;
$data [' filedir '] = $fileDir;

if ($error ==0) {
/*echo ' upload success! '
Echo ' preview: ';
echo "<br>";
echo ' file name '. $img [2];*/
$data [' error '] = 0;
Exit ("<script>parent.callback Json_encode ($data)."); </script> ");

}elseif ($error ==1) {
$data [' ERROR '] = 1;
$data [' meg '] = ' exceeds the file size, set in php.ini file ';
Exit ("<script>parent.callback Json_encode ($data)."); </script> ");
}elseif ($error ==2) {
$data [' error '] = 2;
$data [' meg '] = ' exceeds the file size max_file_size option specified value ';
Exit ("<script>parent.callback Json_encode ($data)."); </script> ");
}elseif ($error ==3) {
$data [' ERROR '] = 3;
$data [' meg '] = ' file is only partially uploaded ';
Exit ("<script>parent.callback Json_encode ($data)."); </script> ");
}elseif ($error ==4) {
$data [' ERROR '] = 4;
$data [' meg '] = ' no file uploaded ';
Exit ("<script>parent.callback Json_encode ($data)."); </script> ");
}else{
$data [' meg '] = ' upload file size is 0 ';
Exit ("<script>parent.callback Json_encode ($data)."); </script> ");
}

}
}else{
$data [' ERROR '] = 4;
$data [' meg '] = ' no file uploaded ';
Exit ("<script>parent.callback Json_encode ($data)."); </script> ");
}
?>
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.