我正在尝试将 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");
?>
我哪里错了? 谢谢
最佳答案
更改此参数:
contentType: XMLDocument,
关于javascript - 如何上传 xml 文件并发送到 php 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19490317/