在通过HTTP请求上传文件的各种测试之后,HTTP请求似乎最适合于非常大的文件+1GB的上传。
下面列出了我为HTTP文件上传请求测试的简单代码,运行良好:
JavaScript:
var req = createRequest();
req.open("PUT", "PHP/filePutLoad.php");
req.setRequestHeader("Content-type", "text/plain");
req.onload = function (event)
{
console.log(event.target.responseText);
}
req.send(aUploadedFile.file_object);
PHP:
include 'ChromePhp.php';
require_once 'mysqlConnect.php';
ini_set('max_execution_time', 0);
ChromePHP::log( '$_PUT :' . print_r($_PUT));
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");
/* Read the data 1 KB at a time and write to the file */
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
/* Close the streams */
fclose($fp);
fclose($putdata);
但是,在将文件从JavaScript上传到PHP时,我在传递参数和变量时遇到了困难。例如,我需要交付上传目标文件夹,其中需要存储新的数据,上传者的ID等等。
谢谢。
发布于 2013-06-11 07:12:43
使用PUT,当您在查询字符串中追加参数时,它也可以工作。我也在寻找另一种方法来解决这个问题。不过,这是我目前正在使用的解决办法
curl -X将"http://www.my-service.com/myservice?param1=val1“--数据@file.txt
https://stackoverflow.com/questions/15160599
复制