I'm writing a web application replacing a c++ program. The goal of the application is to add or take pictures from the browser and then uploading it to a server.
The uploading of a file has two primary steps, checking if file already exists on the server and if not, uploading the file.
I'm wondering how I can make the first primary step in Ajax (sending a file (PHP script is using $_FILES which I can't change) without the actual file content).
The c++ code looks like this:
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("
");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"expected_size\"
");
*dataBuffer_ += fileSize.toAscii() + QByteArray("
");
//original size
//Original size of the file before compression and encoding
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("
");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"original_size\"
");
*dataBuffer_ += QString::number(file.size()).toAscii() + QByteArray("
");
//tell upload.php that we have a file to upload
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("
");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"mode\"
");
*dataBuffer_ += QByteArray("check_file
");
//the binary
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("
");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"file\"; filename=\"") + file.fileName().toAscii() + QByteArray("\"
");
*dataBuffer_ += QByteArray("content-type: application/zip
");
*dataBuffer_ += fileBuffer;
*dataBuffer_ += QByteArray("
");
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("--");
I would like to do something like this:
var form = new FormData();
form.append("expected_size", expected_size);
form.append("original_size", original_size);
form.append("mode", "check_file");
form.append("file", fileInput.files[0]);
var xhr = new XMLHttpRequest();
xhr.open('POST', action, true);
xhr.send(formData);
This script sends the file information with the actual file, while I only want to send the file information. Anybody knows how I can do this?
Here is the PHP script (which I can't modify):
//check if the file was already uploaded to the server (for resuming an upload)
//returns file:exist or file:not_exist
//the post is the same as when transmitting a file, except the data block
if ('check_file' == $_POST['mode'])
{
echo "CHECK FILE
";
if (!file_exists($_SESSION['localdir']))
{
echo "upload:fail
";
echo "message:Unable to copy data on server
";
exit();
}
if (empty($_FILES))
{
echo "upload:fail
";
echo "message:No files were uploaded to the server
";
exit();
}
$file = $_FILES['file'];
$path = $_SESSION['localdir'] . "/" . $file['name'];
if (file_exists($path))
{
echo $file['name'] . " already exists on the server
";
echo "Server file size: " . filesize($path) . "
";
echo "Uploaded file size: " . $_POST['original_size'] . "
";
if (filesize($path) == $_POST['original_size'])
{
echo "file:exist
";
//add the file to the global list if it is not already there
if (!array_search($file['name'], $_SESSION['filenames'], true)) //true=strict
array_push($_SESSION['filenames'], $file['name']);
}
else
echo "file:not_exist
";
}
}