I'm testing this code that I found somewhere on the web. I also tried code that I found in this answer, but nothing seems to be working as I want it to. Here is my javascript:
function fileUpload(url, fileData, fileName)
{
var fileSize = fileData.length,
boundary = "---------------------------7da24f2e50046",
xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
// simulate a file MIME POST request.
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);
xhr.setRequestHeader("Content-Length", fileSize);
var body = "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name='file'; filename='" + fileName + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += fileData + "\r\n";
body += "--" + boundary + "--";
alert(body);
xhr.onreadystatechange = function ()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
alert(xhr.responseText);
}
}
xhr.send(body);
return true;
}
function send()
{
fileUpload("receive.php", "abcdefg", "foo.txt");
}
And this is my "receive.php" file:
<html>
<head>
</head>
<body>
<?php
foreach ($_POST as $var => $value) echo "$var = $value<br>n";
?>
</body>
</html>
I'm getting the "receive.php" HTML back but without anything being echo
ed. But if I actually send a file from an HTML form it echo
s the file name.
I'm also trying to use Fiddler2 to intercept some real file uploads to see what the POST message is, but I can't seem to find the file content anywhere in the message. I figured the file's content might be encoded somehow so I tried sending a pretty large file, about 2MB, to see if I can find some big chunk of data anywhere in the message being sent, but I didn't. I just see the header and the file name.
Am I missing something?
Edit I really feel I'm missing something obvious. Here is a screenshot of Fiddler2 when I upload a real file:
The content length is the size of the string at the bottom, which doesn't include the file's content but just the file name. The function I'm using and every javascript code that does what I'm trying to do set the content length to a string that includes the file's content and the boundary and other information as you can see in my code above. So my function is creating a POST message that has a different structure that the POST message that Fiddler is showing me.
And where in the world is the file content. Another weird thing is that when I send a 2MB size file from IE, the page navigates to the page that receives the file almost instantaneously, as if nothing is being really sent. I'm very confused.
Edit2 I messed up. There was something wrong with the FORM I was using to test uploads. I'm using another form and now I can see the file contents in Fiddler. I think is probably because I was missing the "enctype='multipart/form-data'" So now I can see why is my javascript not working. I'll let you know when I find a solution.