5 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
<?php
// Count total files
var_dump($_FILES['files']);
$countfiles = count($_FILES['files']['name']);
echo $countfiles;
// Upload directory
$upload_location = "uploads/";
// To store uploaded files path
$files_arr = array();
// Loop all files
for($index = 0;$index < $countfiles;$index++){
echo $index;
// File name
$filename = $_FILES['files']['name'][$index];
echo $filename;
// Get extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
echo $ext;
// Valid image extension
$valid_ext = array("png","jpeg","jpg", "csv");
// Check extension
if(in_array($ext, $valid_ext)){
// File path
$path = $upload_location.$filename;
// Upload file
if(move_uploaded_file($_FILES['files']['tmp_name'][$index],$path)){
$files_arr[] = $path;
}
}
}
/* echo json_encode($files_arr); */
die;
const Axios = require('axios');
const Fs = require('fs');
const FormData = require('form-data');
const allFiles = [
{uri: 'my/Spark_Dataset/combin.csv'},
{uri: 'my/Spark_Dataset/combine-csv.csv'},
// {uri: 'C:\\Users\\my\\combine-csv.csv'}
];
(async function() {
var formData = new FormData();
//here allFiles is an array of object , each object contains 1 file with details like local file path ,name,size
for (var index = 0; index < allFiles.length; index++) { //Append multiple files to formData.
// console.log(index);
let currentFile = allFiles[index]
//createReadStream Retyrns fileData
// console.log(currentFile);
let fileWithInfo = await Fs.createReadStream(currentFile.uri)
formData.append("files[]", fileWithInfo );
}
console.log(formData);
Axios.post('http://localhost:8000/upload.php',
formData,
{
headers: formData.getHeaders(),
// 'Content-Type':'multipart/form-data',
})
.then(d => console.log("DONE=>>>> ", d.data))
.catch((f) => console.log('FAILED=>> ', f))
})();
packages.json
"axios": "^0.20.0",
"form-data": "^3.0.0",
"fs": "0.0.1-security",
首先我啟動(dòng)了本地服務(wù)器php -S localhost:8000
運(yùn)行本地服務(wù)器后,我運(yùn)行 index.js 文件,node index.js
我在本地測(cè)試了代碼,它工作正常。

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
Axios.post('example.com/upload.php', formData, {
headers: formData.getHeaders()
})
.then(d => console.log("DONE=>>>> ", d.data))
.catch((f) => console.log('FAILED=>> ', f));
您缺少axios post 請(qǐng)求中您的情況,您可以直接傳遞 formData 或調(diào)用 formData.getBuffer() data。formData

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
https://github.com/form-data/form-data#buffer-getbuffer
const { readFile } = require('fs/promises');
for (let index = 0; index < allFiles.length; index++) {
const currentFile = allFiles[index];
const fileWithInfo = await readFile(currentFile.uri);
formData.append('files[]', fileWithInfo);
}
axios.post('https://example.com/', formData.getBuffer(), formData.getHeaders());
// you have to send files as buffer
還有流 https://stackoverflow.com/a/53126159/8784402的解決方案
const response = await axios({
method: 'post',
url: 'http://www.yourserver.com/upload',
data: formData,
headers: {
'content-type': `multipart/form-data; boundary=${form._boundary}`,
},
});

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
也許我的回答沒(méi)有解決,但我建議對(duì) PHP 代碼進(jìn)行一些更改。當(dāng)然,$_FILES 數(shù)組中的鍵不能是數(shù)字,最好使用 foreach 而不是 for。我允許自己稍微更改您的代碼以使其更小
<?php
// Upload directory
$upload_location = "uploads/";
// To store uploaded files path
$files_arr = array();
// Valid image extension
$valid_ext = array("png","jpeg","jpg");
foreach($_FILES['files']['name'] as $key => $file) {
if(
in_array(pathinfo($_FILES['files']['name'][$key], PATHINFO_EXTENSION), $valid_ext)
&&
move_uploaded_file($_FILES['files']['tmp_name'][$key],$upload_location.$_FILES['files']['name'][$key])
) {
$files_arr[] = $upload_location.$_FILES['files']['name'][$key];
} else {
//Only for debugging
$files_arr[] = 'Not uploaded: '.$upload_location.$_FILES['files']['name'][$key];
}
}
echo json_encode($files_arr);
die;

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
您allFiles.length
在 NodeJS 代碼中錯(cuò)過(guò)了循環(huán)中的參數(shù)。正確的代碼將是
for (var index = 0; index < allFiles.length; index++) {
PHP代碼是正確的,但是如果你發(fā)送一個(gè)空的files數(shù)組,它不會(huì)出錯(cuò)并且會(huì)返回成功響應(yīng)!
添加回答
舉報(bào)