我正在尝试将 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});
}
}
警报($('form')[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");
?>
我哪里错了?谢谢