2013-10-21 61 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

對我來說也是一樣,但是我怎麼能拿到這個文件的字符串呢?對不起,我是一個新手 –

相關問題