0
我在寫一個web應用程序來替換C++程序。該應用程序的目標是從瀏覽器添加或拍照,然後將其上傳到服務器。Ajax:發送沒有實際文件的文件信息
上傳文件有兩個主要步驟,檢查服務器上是否已存在文件,如果沒有,則上傳文件。
我想知道如何在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";
}
}
如果您只關心文件名和文件大小,可以將'files [0] .name'和'files [0] .size'發送到服務器。 – Passerby 2013-04-11 10:17:44
你的意思是像form.append(「file」,fileInput.files [0] .name)?我不知道這是可能的!謝謝,我會試試:)。 – 2013-04-11 10:23:43
請注意,'files [0] .name'是一個**字符串**,'files [0] .size'是一個**整數**。你將不得不修改你的服務器端代碼來滿足這個變化(因爲沒有'$ _FILES'可用)。 – Passerby 2013-04-11 10:27:19