The native ajax and iframe frameworks can be used to upload image files,
You should be able to cite several common implementation methods of asynchronous file upload functions. The most frequently used methods include Native ajax and iframe frameworks to upload image files, the following are two ways to upload an image file: Native ajax and iframe. The content is as follows:
Method 1: Use the iframe framework to upload images
The html code is as follows:
<div class="frm"><form name="uploadFrom" id="uploadFrom" action="upload.php" method="post" target="tarframe" enctype="multipart/form-data"><input type="file" id="upload_file" name="upfile"></form><iframe src="" width="0" height="0" style="display:none;" name="tarframe"></iframe></div><div id="msg"></div>
Index. js file:
$(function(){$("#upload_file").change(function(){$("#uploadFrom").submit();});});function stopSend(str){var im="
Upload. php file:
<Php $ file = $ _ FILES ['upfile']; $ name = rand ). dechex (rand (0,10000 )). ". jpg "; move_uploaded_file ($ file ['tmp _ name']," upload/images /". $ name); // call the js function echo "<script> parent. stopSend ('$ name') </script> ";?>
Method 2: Original Ecological ajax File Upload
<! DOCTYPE html>
Php code:
<?phpif(isset($_FILES["myfile"])){$ret = array();$uploadDir = 'images'.DIRECTORY_SEPARATOR.date("Ymd").DIRECTORY_SEPARATOR;$dir = dirname(__FILE__).DIRECTORY_SEPARATOR.$uploadDir;file_exists($dir) || (mkdir($dir,0777,true) && chmod($dir,0777));if(!is_array($_FILES["myfile"]["name"])) //single file{$fileName = time().uniqid().'.'.pathinfo($_FILES["myfile"]["name"])['extension'];move_uploaded_file($_FILES["myfile"]["tmp_name"],$dir.$fileName);$ret['file'] = DIRECTORY_SEPARATOR.$uploadDir.$fileName;}echo json_encode($ret);}?>
The above is all the content in this article. I hope you can learn and understand the ajax and iframe frameworks to help upload image files.