dqsh30374 2013-04-11 10:05
浏览 25
已采纳

Ajax:发送没有实际文件的文件信息

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


 }
  • 写回答

1条回答 默认 最新

  • duanrongpai9556 2013-04-29 17:51
    关注

    I've done it by setting my headers manually (like the c++ code above), my AJAX:

    var boundary = generateBoundary();
    
    var databuffer = "
    ";
    // set original_size
    databuffer += "--" + boundary + "
    ";
    databuffer += "content-disposition: form-data; name=\"original_size\"
    
    ";
    databuffer += allFiles[index].size + "
    ";
    // set mode
    databuffer += "--" + boundary + "
    ";
    databuffer += "content-disposition: form-data; name=\"mode\"
    
    ";
    databuffer += "check_file
    ";
    // set the file
    databuffer += "--" + boundary + "
    ";
    databuffer += "content-disposition: form-data; name=\"file\"; filename=\"" + allFiles[index].name + "\"
    ";
    databuffer += "content-type: application/zip
    
    ";
    databuffer += ""; // this is de file content => empty
    databuffer += "
    ";
    // end
    databuffer += "--" + boundary + "--";
    

    This sends the file with the needed information without the actual file content. I hope this helps other people.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器