In the form can not directly use Ajax to upload files, the idea is to use the plugin to upload files without refreshing, return the file upload address, and then the address as an Ajax parameters to the server side for database processing. You can use the Uploadify plugin to implement this idea.
Official website: http://www.uploadify.com
Document Address: http://www.uploadify.com/documentation/
Plug-ins have Flash version and HTML5 version, the project is used in the Flash version.
Download Demo and Unzip:
Where index.php: Contains the HTML, and called the Core JS library interface;
Jquery.uploadify.js: Core JS Library;
uploadify.php: Handles the upload and returns the status value of whether the upload was successful.
Make a minor change to the official demo:
Index.html Main code
<!DOCTYPE HTML><HTML><Head><Linkrel= "stylesheet"href= "__public__/css/uploadify.css"type= "Text/css"> <Scriptsrc= "__public__/js/jquery-1.8.3.js.js"></Script><Scriptsrc= "__public__/js/jquery.uploadify.js"type= "Text/javascript"></Script><Scriptsrc= "__public__/js/upload.js"type= "Text/javascript"></Script></Head>
<Body><formID= "Upform"style= "Display:none"> <DivID= "queue"></Div> <inputID= "File_upload"name= "File_upload"type= "File"multiple= "true"></form></Body></HTML>
Upload.js Code:
$(function() {$timestamp= $ ("#timestamp"). html (); $token= $ ("#token"). html (); $swfurl= $ ("#swfurl"). html (); $(' #file_upload '). uploadify ({' FormData ' : { ' Timestamp ': $timestamp,' token ': $token},' SwF ': $swfurl,' Uploader ': ' uploadify.php ', ' Onuploadsuccess ':function(file, data, response) {varDataobj = eval ("(" + Data + ")"); if(1 = =Dataobj.code) {$msg= ' file ' + file.name + ' upload successful '; $("#msg"). HTML ($msg); $("#filename"). HTML (dataobj.filename); }Else if(2 = =Dataobj.code) {$msg= ' File upload failed, please upload less than 2M. Dox,. docx,. pdf Files '; Layer.msg ($msg,2, 3); }Else if(3 = =Dataobj.code) {$msg= ' File type error '; Layer.msg ($msg,2, 3); }Else{$msg= ' upload failed, please upload less than 2M. Dox,. docx,. pdf Files '; Layer.msg ($msg,2, 3); } } }); })
uploadify.php
<?PHP$targetFolder= './uploads/authorization '; $verifyToken=MD5(' Unique_salt '.$_post[' Timestamp ']);if(!Empty($_files) &&$_post[' token '] = =$verifyToken) { $tempFile=$_files[' Filedata '] [' Tmp_name ']; $targetPath=$_server[' Document_root '].$targetFolder; $fileTypes=Array(' Doc ', ' docx ', ' PDF '); $fileParts=PathInfo($_files[' Filedata '] [' Name ']); $filename=$_session[' member '] [' mid ']. ' -‘.Date(' Ymd-his '). ' -‘.Mt_rand(0,1000). '. '.$fileParts[' extension ']; $targetFile=RTrim($targetPath,‘/‘) . ‘/‘ .$filename; if(In_array($fileParts[' extension '],$fileTypes)) { if(Move_uploaded_file($tempFile,$targetFile)){ $arr[' Code ']= 1; }Else{ //Upload failed $arr[' Code '] = 2; $arr[' file_name '] = '; } } Else { //File type error $arr[' Code '] = 3; $arr[' file_name '] = '; }}Else{ $arr[' Code '] = 4; $arr[' file_name '] = ';}$arr[' filename '] =$filename;Exit(Json_encode ($arr));
Ajax no flush upload file plugin uploadify use