dongqiao8502 2017-02-09 13:37
浏览 43

使用图像文件从Node.js POST到PHP

I am trying to POST some data and a file from a Node.js application to a PHP script. I am unable to get the file from PHP side. The request goes through and I get 200 back but PHP thinks the $_FILES array is empty.

My code is

var options = {
headers: {
        'Content-Type': 'multipart/form-data'
    }
};
var postData = querystring.stringify(req.body);
var contentLength = postData.length;
options.body = postData;
if (req.files) {
    options.files = { userfile: req.files.userfile };
}
options.headers['Content-Length'] = contentLength;
options.uri = appConfig.url + urlMapping[req.url];
options.method = req.method;

request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        res.send(JSON.parse(body));
    }
});

Data send to php api

{ headers: { 'Content-Type': 'multipart/form-data', 'Content-Length': 155 },
  body: 'first_name=Raju&last_name=Mathai&email_address=rajumathai%40gm.com&mobile_number=9745342931&password=12345&state=Kerala&date_of_birth=12%2F12%2F1988&type=1',
  files: 
   { userfile: 
      { fieldName: 'userfile',
        originalFilename: 'man.png',
        path: '/tmp/CPu0ul670BTtgYXKU07Je_2c.png',
        headers: [Object],
        size: 11288,
        name: 'man.png',
        type: 'image/png' } },
  uri: 'http://192.168.1.102/byker-api-server/webservices/try_new_provider_sign_up.php',
  method: 'POST' }

PHP code

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$mobile_number = $_POST['mobile_number'];
$password = $_POST['password'];
$state = $_POST['state'];
$date_of_birth = $_POST['date_of_birth'];
$type = $_POST['type'];

$uploadDir = "./uploads/";      //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);

I am using multiparty for parsing form data

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器