我正在编写一个替换c ++程序的Web应用程序。该应用程序的目标是从浏览器添加或拍摄照片,然后将其上传到服务器。
上传文件有两个主要步骤,检查服务器上是否已存在文件,如果没有,则上传文件。
我想知道如何在Ajax中创建第一个主要步骤(发送文件(PHP脚本使用$ _FILES,我无法更改),而没有实际的文件内容。)
c ++代码如下所示:
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("\r\n");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"expected_size\"\r\n\r\n");
*dataBuffer_ += fileSize.toAscii() + QByteArray("\r\n");
//original size
//Original size of the file before compression and encoding
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("\r\n");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"original_size\"\r\n\r\n");
*dataBuffer_ += QString::number(file.size()).toAscii() + QByteArray("\r\n");
//tell upload.php that we have a file to upload
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("\r\n");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"mode\"\r\n\r\n");
*dataBuffer_ += QByteArray("check_file\r\n");
//the binary
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("\r\n");
*dataBuffer_ += QByteArray("content-disposition: form-data; name=\"file\"; filename=\"") + file.fileName().toAscii() + QByteArray("\"\r\n");
*dataBuffer_ += QByteArray("content-type: application/zip\r\n\r\n");
*dataBuffer_ += fileBuffer;
*dataBuffer_ += QByteArray("\r\n");
*dataBuffer_ += QByteArray("--") + QByteArray(MULTIPART_BOUNDARY) + QByteArray("--");
我想做这样的事情:
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);
此脚本使用实际文件发送文件信息,而我只想发送文件信息。谁知道我怎么能这样做?
这是PHP脚本(我无法修改):
//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\n";
if (!file_exists($_SESSION['localdir']))
{
echo "upload:fail\n";
echo "message:Unable to copy data on server\n";
exit();
}
if (empty($_FILES))
{
echo "upload:fail\n";
echo "message:No files were uploaded to the server\n";
exit();
}
$file = $_FILES['file'];
$path = $_SESSION['localdir'] . "/" . $file['name'];
if (file_exists($path))
{
echo $file['name'] . " already exists on the server \n";
echo "Server file size: " . filesize($path) . "\n";
echo "Uploaded file size: " . $_POST['original_size'] . " \n";
if (filesize($path) == $_POST['original_size'])
{
echo "file:exist\n";
//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\n";
}
}
答案 0 :(得分:0)
我是通过手动设置标题(如上面的c ++代码),我的AJAX来完成的:
var boundary = generateBoundary();
var databuffer = "\r\n";
// set original_size
databuffer += "--" + boundary + "\r\n";
databuffer += "content-disposition: form-data; name=\"original_size\"\r\n\r\n";
databuffer += allFiles[index].size + "\r\n";
// set mode
databuffer += "--" + boundary + "\r\n";
databuffer += "content-disposition: form-data; name=\"mode\"\r\n\r\n";
databuffer += "check_file\r\n";
// set the file
databuffer += "--" + boundary + "\r\n";
databuffer += "content-disposition: form-data; name=\"file\"; filename=\"" + allFiles[index].name + "\"\r\n";
databuffer += "content-type: application/zip\r\n\r\n";
databuffer += ""; // this is de file content => empty
databuffer += "\r\n";
// end
databuffer += "--" + boundary + "--";
这将发送包含所需信息的文件,而不包含实际文件内容。 我希望这有助于其他人。