分类:原创 share 发布 2010.05.27 07:05:31Tag : html5,css3,php commentComment:0
7
dig bury

JS代码

        /************** html5上传初始化 ************************/
         var dropbox = document.getElementById("dropbox");
         dropbox.addEventListener("dragenter", dragenter, false);
         dropbox.addEventListener("dragover", dragover, false);
         dropbox.addEventListener("drop", drop, false);

/************** html5上传函数 ************************/
    function dragenter(e) {
      e.stopPropagation();
      e.preventDefault();
    }

    function dragover(e) {
      e.stopPropagation();
      e.preventDefault();
    }

    function drop(e) {
      e.stopPropagation();
      e.preventDefault();

      handleFiles(e.dataTransfer.files);
      return false;
    }
   /************** html5上传关键函数 ************************/  
 function handleFiles(files) {
        
    for(var i=0; i < files.length; i++) {
        
            
            this.xhr = new XMLHttpRequest();
            this.xhr.upload.addEventListener("progress", function(e) {    
                if (e.lengthComputable) {    
                    var percentage = Math.round((e.loaded * 100) / e.total);  
                    
                    //变量:percentage为上传进度,由0-100;  
                    //可以用来做进度条  
            }    
            }, false);   
            
            this.xhr.upload.addEventListener("load", function(e){   
                alert("上传完成");
                refresh_page();
                //上传完成事件!  
            }, false);
            
            this.xhr.open("POST", website+"/admin/os/upload_file_html5.php?name="+files[i].name+"&id="+jQuery.query.get('id'));         
            this.xhr.overrideMimeType('text/plain; charset=utf-8');  
            //this.xhr.setRequestHeader('Content-Type', 'multipart/form-data');
            this.xhr.setRequestHeader('Content-Type','multipart/form-data; charset=utf-8');
            this.xhr.send(files[i]);
            
         
         }
        
      }

PHP代码

<?php
            header("Content-Type:text/html; charset=utf-8");
            $file_content = file_get_contents('php://input'); // 读取收到的文件内容
            
            
            
            //保存进数据库
            $auto_id = $mysql_link->select(" SHOW TABLE STATUS WHERE Name = 'lw_os_menu' ");
            $auto_id = $auto_id[0]->Auto_increment;
            //$mssage = upfile($_FILES['file_name'],"$webroot/admin/os/user/$_COOKIE[$cookie_user]/upload/",$auto_id);
            $file_name = $_GET[name];
                //获取文件后缀,匹配相对应的图标
            $type = explode(".",$file_name);
            $type = $type[count($type)-1];
            $type = strtolower($type);//大写转小写
            //把音乐文件转换成wav格式
            //if($type == 'mp3' || $type == 'wma'){
            //    $type = 'wav';
        //    }
            file_put_contents("$webroot/admin/os/user/$_COOKIE[$cookie_user]/upload/".$auto_id.".".$type,$file_content);
            $image = typeChooseIco($type);//获取对应图片的ico    
            $url = "admin/os/user/$_COOKIE[$cookie_user]/upload/$auto_id.".$type;    
                $mysql_link->insert("lw_os_menu",array ("id"=>"NULL","parent_id"=>"$_GET[id]","name"=>"$file_name","url"=>"$url","level"=>"","ico"=>"","image"=>$image,"user"=>"$_COOKIE[$cookie_user]","sort"=>"file"));
            
?>
我也来说2句,请不要发表违反法律的言论

姓名

Email(必填)

主页(不要加http://)