Iframe: in fact, the upload program page is opened on the current page, a bit like a partial ajax refresh, but we put it to the iframe page for upload, at the same time, the iframe page is hidden.
A simple iframe upload example:
In this example, the iframe name is used, so the link is opened in the iframe when the form is submitted
Feel refreshed)
Call the startUpload method when submitting a form. Of course, this is defined by JS.
The front page index.html is mainly a form and a js callback function. When uploading a file, the method and enctype attributes of the form must be the same as the following code. Set the target value to the name of iframe, so that you can upload files without any need.
| The Code is as follows: |
Copy code |
|
<Title> upload a file </title>
<Script>
Function CallbackFunction (str ){
Alert ("uploaded successfully ");
}
</Script>
<Form action = "uploadfile. php" enctype = "multipart/form-data" method = "post" target = "iframeUpload">
<Iframe name = "iframeUpload" src = "" width = "350" height = "35" frameborder = 0 SCROLLING = "no" style = "display: NONE "> </iframe>
<Input id = "test_file" name = "test_file" type = "file">
<Input value = "Upload File" type = "submit">
</Form> |
Uploadfile. php on the background upload processing page. This code is a simple upload code without error and exception handling. After the upload code is executed, you need to tell the parent page that the upload has been completed. Therefore, you can call the callback function CallbackFunction of the parent page on this page. This function can be defined as a parameter.
| The Code is as follows: |
Copy code |
|
<? Php
Set_time_limit (0 );
If ($ _ SERVER ['request _ method'] = 'post '){
Move_uploaded_file ($ _ FILES ["test_file"] ["tmp_name"],
Dirname ($ _ SERVER ['script _ filename']). "/UploadTemp/". $ _ FILES ["test_file"] ["name"]);
Echo "<script> window. parent. CallbackFunction (); </script> ";
}
?> |
The above is a simple method to use iframe to implement the upload of files without any need. If you want a robust program, you need to refine it.
Example 2
PHP uses iframe to upload files and return values to the parent framework
In fact, fresh water uses this method. I can see that my shoes blog is written in this way. You can easily paste it.
| The Code is as follows: |
Copy code |
|
If ($ result)
{
Echo "Upload successful! File Path: ". $ file2;
Echo '<input name = "img" type = "hidden" value = "'. $ file2. '" id = "img"/> ';
Echo '<script> parent.doc ument. form1.img. value = document. getElementById ("img"). value; </script> ';
} |
After the file is uploaded successfully, write the file path to a hidden domain (img), and then use the DOM principle to send the VALUE in the hidden domain to the img text box in the form form1 of the parent framework:
OK. Let's take a look at the fresh water.
The freshwater editor uses tinyMCE, so there is a little difference.
My form page:
| The Code is as follows: |
Copy code |
|
<Script src = "tiny_mce/tiny_mce.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
// O2k7 skin
TinyMCE. init ({
// General options
Mode: "exact ",
Elements: "content ",
Theme: "advanced ",
Skin: "o2k7 ",
Language: "zh ",
Relative_urls: false,
//... Skip part...
});
Function InsertHtml (type, path ){
Type = type. toLowerCase ()
Switch (type ){
Case '.gif ':
Thecode = ' ';
Break;
Case '.jpg ':
Thecode = ' ';
Break;
// ...... Skip part ......
Default:
Thecode = '<a href = "' + path + '" target = "_ blank"> Download </a> ';
Break;
}
TinyMCE.exe cCommand ('mceinsertcontent', true, thecode );
}
</Script> |
Iframe is in this page.
In the php process submitted by my iframe:
| The Code is as follows: |
Copy code |
|
$ PasteJS = "<script type = text/javascript> n ";
$ PasteJS. = "parent. InsertHtml (" ". $ file_ext." "," ". $ upload_src." "); n ";
$ PasteJS. = "</script> n ";
Echo $ pasteJS; |
Therefore, the uploaded file is moved to the tinyMCE editing box. The file path of upload is also saved through userdata until it is written to the database. The path is stored in the php processing file submitted by iframe. Here, there will be no more fresh water.