如何上传 XML 文件并发送到 PHP 服务器


how to upload an xml file and send to a php server

我正在尝试将其发送到我的php服务器 xml 文件的内容

在我的 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});
    }
}

alert($('form')[0]); return : [object 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,

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