php - 发出包含发布数据和文件上传的 http 多部分请求

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

我正在尝试向一个包含 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/

相关文章:

php - 使用 PHP 发送电子邮件(防止被放入垃圾邮件文件夹)

java - 从 java 端将文件上传到 REST 可以,但从 python 不行

python - 向 API 发送文件时出现问题

java - Spring Boot 不允许对静态资源进行 POST 请求

javascript - PHP/Ajax/JQuery 响应无法正常工作

PHP echo 内联 CSS 背景 url

php - 在取消设置 mysql 对象之前释放结果并关闭连接,或者只是取消设置会破坏对象 n 释放资源?

php - 如何使用 Laravel 5.1 将上传的文件名保存在表中

java - 日文和中文的上传文件名编码问题

android - 发布 Json 对象数据以在 android 中使用 volley 获取 Json 数组响应