To upload a non-refreshing file, we use the iframe framework to upload the file. The other is to use ajax to upload the file, both methods can enable the upload function of the brushless newest file.
A simple iframe upload example:
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 |
|
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> upload a file </title>
</Head>
<Body>
<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>
</Body>
</Html> |
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.