0

我正在编写一个替换 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";
    }


 }
4

1 回答 1

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 + "--";

这会发送带有所需信息的文件,但没有实际的文件内容。我希望这对其他人有所帮助。

于 2013-04-29T17:51:01.903 回答