2

I'm trying to send it to my php server the contents of an xml file

in my html page i'm insert

 <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>   

and this is the 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});
    }
}

the alert($('form')[0]); return : [object HTMLFormElement]

javascript sends everything to the php server but does not receive nothing because write on an file only a white space

this is the code of the php server:

<?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");
?>

where am I wrong? thx

2 Answers 2

2

Change this parameter:

contentType: XMLDocument,
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, try to use $_FILES instead of file_get_contents('php://input').

3 Comments

Then, maybe you can just read file with javascript into variable as string and send it (as string) to the server?
does the same for me, but how could I take the string of the file? sorry I'm a newbie

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.