2013-10-21 89 views
2

我试图将它发送给我的PHP服务器的XML文件如何上传一个XML文件,发送到PHP服务器

的内容在我的html页面我是插入

<form enctype="multipart/form-data">   
      <input class='btn btn-warning' id="file" name="file" type="file" /> 
      <input id="uploadfile" type="button" value="Upload" /> 
</form> 
<progress></progress> 

这是JavaScript

$("#uploadfile").hide(); 
       $(':file').change(function(){ 
       var file = this.files[0]; 
       name = file.name; 
       size = file.size; 
       type = file.type; 
       //Your validation 
       if(type=="text/xml"){ 
        $("#uploadfile").show(); 
       }else{$("#uploadfile").hide();} 
     }); 

$('#uploadfile').click(function(){ 
    var formData = new FormData($('form')[0]); 
    alert($('form')[0]); 
    $.ajax({ 
     url: 'upload.php', //Server script to process data 
     type: 'POST', 
     //Ajax even 
     success: function(data){alert(data)}, 
     error: function(){}, 
     // Form data 
     data: formData, 
     //Options to tell jQuery not to process data or worry about content-type. 
     cache: false, 
     contentType: false, 
     processData: false 
    }); 
}); 

function progressHandlingFunction(e){ 
    if(e.lengthComputable){ 
     $('progress').attr({value:e.loaded,max:e.total}); 
    } 
} 

警报($( '形式')[0]);返回:对象HTMLFormElement]

的JavaScript将一切交给PHP的服务器,但没有收到任何操作,因为在一个文件只有一个空格

写这个是PHP服务器的代码:

<?php 
header('Access-Control-Allow-Origin: *'); 
$postText = file_get_contents('php://input'); 
$datetime=date('ymdHis'); 
$xmlfile = "myfile" . $datetime . ".txt"; 
$FileHandle = fopen($xmlfile, 'w') or die("can't open file"); 
fwrite($FileHandle, $postText); 
fclose($FileHandle); 
echo("grazie e arrivederci"); 
?> 

我在哪里错了? thx

回答

2

更改这个参数:

contentType: XMLDocument, 
+1

功能完美。 Thx:D –

0

首先,尝试使用$_FILES而不是file_get_contents('php:// input')。

+0

无关,结果是一样的 –

+0

然后,也许你可以只读取JavaScript文件到变量作为字符串,并将其发送(如字符串)到服务器? –

+0

对我来说也是一样,但是我怎么能拿到这个文件的字符串呢?对不起,我是一个新手 –

相关问题