2011-05-14 51 views
0

上传文件我有脚本:与AJAX

var xmlhttp; 
    var params = "file=Not sure what is it"; 
    if (window.XMLHttpRequest){ 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else{ 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function(){ 
     if(xmlhttp.readyState==4){ 
      if (xmlhttp.status==200) 
       alert('Upload done'); 
      else 
       alert('Error!!!'); 
     } 
    } 
    xmlhttp.open("POST", "http://localhost/ajax2.php", true); 
    //Send the proper header information along with the request 
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    xmlhttp.setRequestHeader("Content-length", params.length); 
    xmlhttp.setRequestHeader("Connection", "close"); 
    xmlhttp.send(params); 

这个脚本发送POST请求到服务器,现在我只是在想,我怎么可以设置PARAMS的文件内容上传?

回答

0

出于安全原因,您无法单独使用JavaScript执行此操作。试试SWFUploadUploadify或类似的。这些是基于Flash的解决方案,您可以通过JavaScript进行交互。