简体   繁体   中英

HTTP PUT Request: Passing Parameters with File

After numerous various tests with uploading files throught HTTP POST Request, it looks that HTTP PUT Requests are the most suitable for very large files +1GB upload.

The below listed simple code I have tested for HTTP PUT file upload request works well:

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);

However, I have difficulties delivering arguments and variables with the file being uploaded from JavaScript to PHP. For example, I need to deliver upload target folder, where the new data needs to be stored, ID of the uploader, etc..

  • Is there a way to combine HTTP PUT Request with HTTP POST to submit arguments?
  • What are my options if I wish to deliver parameters from JavaScript to PHP along HTTP PUT file upload?

Thank you.

using PUT also, it works when you append the parameters in the query string. I'm also looking for another way for this. Although, this is a workaround I'm using currently

curl -X PUT " http://www.my-service.com/myservice?param1=val1 " --data @file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM