我正在尝试向一个包含 post 数据和文件上传的 php 文件发送一个 post 请求,这里是我要发送的内容、设置 header 的代码、我拥有的 php 文件以及什么我正在从 php 文件返回:([图像文件内容] 是我发送的图像文件的二进制数据的占位符)
我的问题是 $_POST 和 $_FILES 都不显示空数组。我正在想办法解决这个问题。
//我发送到服务器的数据:
--boundary content-disposition: post; name='param1' value1 --boundary content-disposition: post; name='field2' value2 --boundary content-disposition: post; name='field3' value3 --boundary content-disposition: form-data; name='file'; filename='app/native/assets/sampleFile.jpg' Content-Type: image/jpg Content-Transfer-Encoding: binary [image file content] --boundary-- -----------------------------------------------------
for my headers, I have:
request.setRawHeader("Content-Type", "mulipart/form-data, boundary=boundary");
//data holds the string above
request.setRawHeader("Content-Length", QString::number(data.length()).toAscii());
php 文件:
<?php
echo "\npost:\n\n";
print_r($_POST);
echo "\nfiles:\n\n";
print_r($_FILES);
?>
php 文件的结果:
post: Array ( ) files: Array ( )
最佳答案
尝试类似的事情
--boundary Content-Disposition: form-data; name="param1" value1 --boundary Content-Disposition: form-data; name="field2" value2 --boundary Content-Disposition: form-data; name="field3" value3 --boundary Content-Disposition: form-data; name="file"; filename="app/native/assets/sampleFile.jpg" Content-Type: image/jpg Content-Transfer-Encoding: binary [image file content] --boundary--
request.setRawHeader("Content-Type", "multipart/form-data; boundary=boundary");
关于php - 发出包含发布数据和文件上传的 http 多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13751958/