Ajax no flush upload file plugin uploadify use

Source: Internet
Author: User

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

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.