DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> HTML基礎知識 >> HTML5詳解 >> HTML5文件上傳功能
HTML5文件上傳功能
編輯:HTML5詳解     
文件上傳的表單部分:xheditor JS中的代碼:
this.Html5Upload=function(inputname,fromfiles,tourl,callback,onProgress)     var xhr,i=0,count=fromfiles.length,allLoaded=0,allSize=0,_this=this;
    for(var j=0;j<count;j++)allSize+=fromfiles[j].fileSize;
    this.remove=function(){if(xhr){xhr.abort();xhr=null;}}
    this.uploadNext=function(sText)       if(sText)// 當前文件上傳完成         allLoaded+=fromfiles[i-1].fileSize;
        returnProgress(0);       if((!sText&#124;&#124;(sText&&callback(sText,i==count)==true))&&i<count)postFile(fromfiles[i++],tourl,_this.uploadNext,function(loaded){returnProgress(loaded);});     this.start=function(){_this.uploadNext();}
    function postFile(fromfile,tourl,callback,onProgress)       xhr = new XMLHttpRequest(),upload=xhr.upload;
      xhr.onreadystatechange=function(){if(xhr.readyState==4)callback(xhr.responseText);};
      if(upload)upload.onprogress=function(ev){onProgress(ev.loaded);};
      else onProgress(-1);//不支持進度
      xhr.open("POST", tourl);
      xhr.setRequestHeader('Content-Type', 'application/octet-stream');
      xhr.setRequestHeader('Content-Disposition', 'attachment; name="'+inputname+'"; filename="'+fromfile.fileName+'"');
      if(xhr.sendAsBinary)xhr.sendAsBinary(fromfile.getAsBinary());
      else xhr.send(fromfile);     function returnProgress(loaded){if(onProgress)onProgress({'loaded':allLoaded+loaded,'total':allSize});} 監 控相關的協議: RequestHeaders
Content-Type  application/octet-stream
Content-Disposition  attachment; name="filedata"; filename="397913361.gif" 再看一下Html5文件上傳部 分,PHP對其上傳處理: if(isset($_SERVER['HTTP_CONTENT_DISPOSITION']))//Html5 上傳   if(preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info))     $temp_name=ini_get("upload_tmp_dir").'\\'.date("YmdHis").mt_rand(1000,9999).'.tmp';
    file_put_contents($temp_name,file_get_contents("PHP://input"));
    $size=filesize($temp_name);
    $_FILES[$info[1]]=array('name'=>$info[2],'tmp_name'=>$temp_name,'size'=>$size,'type'=>'','error'=>0); }
後 面轉化為相同PHP的文件上傳方式[注意此時的 is_uploaded_file()返回為假,同時move_uploaded_file()也無效,這時候用rename()函數來移動文件]
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved