创建具有发布数据和文件上载的http多部分请求

时间:2012-12-06 20:28:03

标签: php file-upload http-post multipartform-data

我正在尝试将帖子请求发送到包含帖子数据和文件上传的php文件,这里是我发送的内容,设置标题的代码,我拥有的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--

-----------------------------------------------------

对于我的标题,我有:

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

1 个答案:

答案 0 :(得分:3)

尝试类似

的内容
--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");