The native ajax and iframe frameworks can be used to upload image files,

Source: Internet
Author: User

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.

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.