【问题标题】:how to upload an xml file and send to a php server如何上传 xml 文件并发送到 php 服务器
【发布时间】:2013-10-29 16:43:45
【问题描述】:

我正在尝试将 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");
?>

我哪里错了? 谢谢

【问题讨论】:

    标签: javascript php jquery file-upload


    【解决方案1】:

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

    【讨论】:

    • 那么,也许您可​​以将带有javascript的文件作为字符串读取到变量中并将其(作为字符串)发送到服务器?
    • 对我也一样,但我怎么能拿到文件的字符串呢?对不起,我是新手
    【解决方案2】:

    更改此参数:

    contentType: XMLDocument,
    

    【讨论】:

    • 功能完美。谢谢:D
    猜你喜欢
    • 2012-06-13
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2021-07-06
    • 2012-09-23
    • 1970-01-01
    • 2017-06-02
    相关资源
    最近更新 更多