[英]Can't upload file from php to nodejs
我的項目有兩個部分。 第一個是 PHP 部分,它獲取文件並將其發送到第二部分。 這是我的代碼:
$file = fopen('file.wav', 'r');
$size = filesize('file.wav');
$ex_id = getExternalId();
$apiurl = 'http://url.to.the.second.part/?id='.$ex_id.'&oneway=true';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
fclose($file);
//apply logic to $result
exit(0);
第二部分是nodejs上的API部分。 我可以處理請求,並從查詢中獲取id
參數,但req.body
未定義:
const express = require('express');
const app = express();
app.put('api.url', (req, res) => {
const externalId = req.query.id;
//do some stuff here...
//and here i'm expecting uploaded file
//but i got nothing here
const file = req.body;
//other logic here...
res.status(200).send(result);
}
我究竟做錯了什么?
您正在使用fopen
fun 打開文件,它返回文件指針資源。 您需要獲取文件的內容,為此您可以使用file_get_contents
function 然后在請求參數中傳遞文件內容。
$file = file_get_contents('file.wav');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.