2
在通過HTTP POST請求上傳文件進行大量各種測試之後,它看起來HTTP PUT請求最適合用於超大文件+ 1GB上傳。HTTP PUT請求:通過文件傳遞參數
下面列出的簡單的代碼,我已經爲HTTP PUT文件上傳請求測試效果很好:
的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等。
- 有沒有一種辦法HTTP PUT請求與HTTP POST聯合提交的參數?
- 如果我希望通過HTTP PUT文件上傳將參數從JavaScript傳遞到PHP,我有什麼選擇?
謝謝。
您是否嘗試過簡單地完全相同的方式添加參數,你會與一個POST?你有什麼「困難」? – Pointy 2013-03-01 14:56:43
必須是POST嗎? req.open(「PUT」,「PHP/filePutLoad.php」+ args); – 2013-03-01 15:00:52
@Pointy:是的,我嘗試創建與POST請求相同的參數提交。發生了什麼事情是,在服務器上,我收到了一個文件,它將我上傳的文件與我上傳的參數組合在一起。換句話說,參數與服務器上的上傳文件合併在一起。 – 2013-03-01 15:24:13